zoukankan      html  css  js  c++  java
  • spring annotation(1)

    通过 @Autowired的使用来消除 set ,get方法 - Spring 2.5 JPA hibernate 使用方法的点滴整理

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

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

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

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

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


    * 修改在原来注入spirng容器中的bean的方法。
         在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.dom4j.Document;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.springframework.beans.factory.annotation.Autowired;
    import com.firemax.test.hibernate.AlcorTCitys;
    import com.firemax.test.hibernate.AlcorTCitysDAO;
    import com.firemax.test.hibernate.AlcorTCountries;
    import com.firemax.test.hibernate.AlcorTCountriesDAO;
    import com.firemax.test.hibernate.AlcorTProvinces;
    import com.firemax.test.hibernate.AlcorTProvincesDAO;
    import com.firemax.test.hibernate.AlcotTDistrict;
    import com.firemax.test.hibernate.AlcotTDistrictDAO;
    public class CountryService {
         private static Log logger = LogFactory.getLog(CountryService.class);
         @Autowired
         private AlcorTCountriesDAO alcorTCountriesDAO;
         @Autowired
         private AlcorTProvincesDAO alcorTProvincesDAO;
         @Autowired
         private AlcorTCitysDAO          alcorTCitysDAO;
         @Autowired
         private AlcotTDistrictDAO       alcotTDistrictDAO;
         public CountryService(){
         }
        
         public void updateCountry(AlcorTCountries alcorTCountries ) throws Exception{
             this.alcorTCountriesDAO.update(alcorTCountries);
         }
         ....
         //这里去掉了哪些DAO 变量的get 和set 方法。
    }

    * 在applicatonContext.xml中 把原来 引用的<porpery >标签也去掉。

             <bean id="CountryService" class="com.firemax.test.service.CountryService">
                      <property name="alcorTCountriesDAO" ref="AlcorTCountriesDAO" />
                      <property name="alcorTProvincesDAO" ref="AlcorTProvincesDAO" />
                      <property name="alcorTCitysDAO" ref="AlcorTCitysDAO" />
                     <property name="alcotTDistrictDAO" ref="AlcotTDistrictDAO" />
              </bean>

    修改成

              <bean id="CountryService" class="com.firemax.test.service.CountryService">
                    
             </bean>

    当然,我们也可以在构造函数上使用@Auwowired 注解 。如果构造函数有两个入参,分别是 bean1 和 bean2,@Autowired 将分别寻找和它们类型匹配的 Bean,将它们作为 CountryService (Bean1 bean1 ,Bean2 bean2) 的入参来创建 CountryService Bean。

  • 相关阅读:
    聊聊 print 的前世今生
    在树莓派里搭建 Lighttpd 服务器
    如何重复执行一条命令直至运行成功?
    手把手教你Windows Linux双系统的安装与卸载
    你以为只有马云会灌鸡汤?Linux 命令行也会!
    Linux 下三种提高工作效率的文件处理技巧
    太高效了!玩了这么久的Linux,居然不知道这7个终端快捷键!
    Linux下分析bin文件的10种方法
    Linux下几个与磁盘空间和文件尺寸相关的命令
    如何让你的脚本可以在任意地方都可执行?
  • 原文地址:https://www.cnblogs.com/yujy/p/2795191.html
Copyright © 2011-2022 走看看