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。

  • 相关阅读:
    [CISCN2019 总决赛 Day2 Web1]Easyweb
    [极客大挑战 2019]Upload
    [SUCTF 2019]EasyWeb
    2020/2/1 PHP代码审计之任意文件读取及删除漏洞
    2020/1/31 PHP代码审计之文件包含漏洞
    [Luogu P1120]小木棍&#183;加强版
    学习笔记·堆优化$mathscr{dijkstra}$
    [LuoguP1462]通往奥格瑞玛的道路($SPFA+$二分)
    [USACO08JAN]电话线$Telephone Lines$(图论$+SPFA+$ 二分答案)
    [USACO06NOV]玉米田$Corn Fields$ (状压$DP$)
  • 原文地址:https://www.cnblogs.com/yujy/p/2795191.html
Copyright © 2011-2022 走看看