`

注解的力量3 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三)

阅读更多

Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法.

 

<script></script> 通过 (一)(二)的介绍。我们已经做了初步的简化程序。
但是在我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。虽然 可以通过eclipse等工具来自动生成。但是还是会引起程序阅读性上的不便。那么既然注解这么强大。是否可以也把他精简掉呢?

当然可以。这个标签就是@Autowired 

Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。

要实现我们要精简程序的目的。需要这样来处理:

  • 在applicationContext.xml中加入:
  1. <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->   
  2.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

  • 修改在原来注入spirng容器中的bean的方法。在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法
    1. /*国家,省份,城市,城区信息维护服务
    2.  * Created on 2008-9-26
    3.  *
    4.  * 徐泽宇 roamer
    5.  */
    6. package com.firemax.test.service;
    7. import java.util.ArrayList;
    8. import java.util.Iterator;
    9. import java.util.List;
    10. import org.apache.commons.logging.Log;
    11. import org.apache.commons.logging.LogFactory;
    12. import org.dom4j.Document;
    13. import org.dom4j.DocumentHelper;
    14. import org.dom4j.Element;
    15. import org.springframework.beans.factory.annotation.Autowired;
    16. import com.firemax.test.hibernate.AlcorTCitys;
    17. import com.firemax.test.hibernate.AlcorTCitysDAO;
    18. import com.firemax.test.hibernate.AlcorTCountries;
    19. import com.firemax.test.hibernate.AlcorTCountriesDAO;
    20. import com.firemax.test.hibernate.AlcorTProvinces;
    21. import com.firemax.test.hibernate.AlcorTProvincesDAO;
    22. import com.firemax.test.hibernate.AlcotTDistrict;
    23. import com.firemax.test.hibernate.AlcotTDistrictDAO;
    24. public class CountryService {
    25.     private static Log logger = LogFactory.getLog(CountryService.class);
    26.     @Autowired
    27.     private AlcorTCountriesDAO  alcorTCountriesDAO;
    28.     @Autowired
    29.     private AlcorTProvincesDAO  alcorTProvincesDAO;
    30.     @Autowired
    31.     private AlcorTCitysDAO          alcorTCitysDAO;
    32.     @Autowired
    33.     private AlcotTDistrictDAO       alcotTDistrictDAO;
    34.     
    35.     public CountryService(){
    36.         
    37.     }
    38.     
    39.     /**修改一个国家信息
    40.      * @param alcorTCountries
    41.      * @throws Exception
    42.      */
    43.     public void updateCountry(AlcorTCountries alcorTCountries ) throws Exception{
    44.         this.alcorTCountriesDAO.update(alcorTCountries);
    45.     }
    46.     
    47.     ....
    48.     //这里去掉了哪些DAO 变量的get 和set 方法。
    49.     
    50.  
    51. }

  • 在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。
    1. <bean id="CountryService" class="com.firemax.test.service.CountryService">
    2.         <property name="alcorTCountriesDAO" ref="AlcorTCountriesDAO" />
    3.         <property name="alcorTProvincesDAO" ref="AlcorTProvincesDAO" />
    4.         <property name="alcorTCitysDAO" ref="AlcorTCitysDAO" />
    5.         <property name="alcotTDistrictDAO" ref="AlcotTDistrictDAO" />
    6.  </bean>
    修改成
    1. <bean id="CountryService" class="com.firemax.test.service.CountryService">
    2.         
    3. </bean>
    当然,我们也可以在构造函数上使用@Auwowired 注解 。如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService  Bean。
在运行一下你的业务程序。如果没有错误。恭喜你。这个步骤我们又完成了。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics