Spring的注入方式详解

来源:互联网 发布:button按钮调用js 编辑:程序博客网 时间:2024/05/18 00:42
  1. Spring的注入方式详解  
  2. Spring有三个注入方式,type1,type2,type3  
  3. type1  接口依赖  
  4. type2  setter/getter  
  5. type3  构造方法  
  6.    
  7. type2,type3较用常用  
  8. 首先来看一下type2  
  9.   
  10.    
  11. type2 setter/getter(引用注入)  
  12.   
  13. 例如,存在一个User类和Home类  
  14.    
  15. user类里一个userName;一个Home对象的属性.  
  16. public class User {   
  17.        private String userName;  
  18.     private Home myHome;  
  19.        public Home getMyHome() {   
  20.               return myHome;  
  21.        }  
  22.        public void setMyHome(Home myHome) {   
  23.               this.myHome = myHome;  
  24.        }  
  25.        public String getUserName() {   
  26.               return userName;  
  27.        }  
  28.        public void setUserName(String userName) {   
  29.               this.userName = userName;  
  30.        }  
  31. }  
  32. public class Home {   
  33.        private String homeAddr;  
  34.        public String getHomeAddr() {   
  35.               return homeAddr;  
  36.        }  
  37.        public void setHomeAddr(String homeAddr) {   
  38.               this.homeAddr = homeAddr;  
  39.        }  
  40. }  
  41. public class TestMain {   
  42.     public static void main(String[] args) {   
  43.         ApplicationContext context=new FileSystemXmlApplicationContext("test/lyx/applicationContext.xml");  
  44.         User user1=(User)context.getBean("user");  
  45.       
  46.         System.out.println(“姓名为: ”+user1.getUserName());  
  47.         System.out.println(“家庭住址是: ”+user1.getMyHome().getHomeAddr());  
  48.    
  49.     }  
  50.    
  51. }  
  52. 将两个bean的注入:(applicationContext.xml配置如下)  
  53.        <bean id="home" class="test.lyx.Home" abstract="false"  
  54.               singleton="true" lazy-init="default" autowire="default"  
  55.               dependency-check="default">  
  56.               <property name="homeAddr">  
  57.                      <value>大连</value>  
  58.               </property>  
  59.        </bean>  
  60.    
  61.        <bean id="user" class="test.lyx.User" abstract="false"  
  62.               singleton="true" lazy-init="default" autowire="default"  
  63.               dependency-check="default">  
  64.               <property name="userName">  
  65.                      <value>liuyuanxi</value>  
  66.               </property>  
  67.               <property name="myHome">  
  68.               <ref bean="home"/>  
  69.               </property>  
  70.        </bean>  
  71. 这里的user bean,引用了home bean,  
  72. 运行时会打出   
  73. 姓名为:liuyuanxi   
  74. 家庭住址是:大连  
  75. 这种方式就是setter/getter方式注入,习惯用JavaBean的人应该很容易理解这种方法,也就是能过<property name="userName">来指定属性. <value>liuyuanxi</value>来指定属性所对应的值.多少个属性就有多少个<property>  
  76. 这里一个人,和家是两个类,也提到了两个bean之间的引用.也就是user bean给名字赋了值,home bean给地址赋了值.如果在user bean中想引入 home bean中的地址.就用<ref/>不再用<values>  
  77.    
  78.    
  79. type3 构造方法注入  
  80.   
  81. 这里我们在User里加入一个构造器  
  82. public User(Home myHome){   
  83. this.myHome=myHome;  
  84. }  
  85. 然后 把配置文件改一下:autowire=constructor;  
  86.    
  87.        <bean id="home" class="test.lyx.Home" abstract="false"  
  88.               singleton="true" lazy-init="default" autowire="default"  
  89.               dependency-check="default">  
  90.               <property name="homeAddr">  
  91.                      <value>大连</value>  
  92.               </property>  
  93.        </bean>  
  94.    
  95.        <bean id="user" class="test.lyx.User" abstract="false"  
  96.               singleton="true" lazy-init="default" autowire=" constructor "  
  97.               dependency-check="default">  
  98.               <property name="userName">  
  99.                      <value>liuyuanxi</value>  
  100.               </property>  
  101.               <property name="myHome">  
  102.               <ref bean="home"/>  
  103.               </property>  
  104.        </bean>  
  105. 运行时会打出   
  106. 姓名为:liuyuanxi   
  107. 家庭住址是:大连  
  108. 这种方式就是构造器注入  
  109.    
  110. 我们再来看看spring的绑定  
  111. 也就是通过bean属性autowire来设置  
  112. 1.       通过bean属性autowire="byType"的设置可以使用bean在运行时候根据去寻找同类型的已定义属性,如果找不到则处于未绑定状态.(已定义好指在applicationContext.xml中初始化)这里我们把配置文件的 user bean的autowire改成autowire="byType",注意一定要把User的构造器去掉,要不然先找构造器,会出错.这里的home bean是属于,test.lyx.Home类型的,而user bean里有两个属性一个属性已经初始化,而另一个属性Home,就会自动找到.  
  113. applicationContext.xml配置如下:  
  114.        <bean id="home" class="test.lyx.Home" abstract="false"  
  115.               singleton="true" lazy-init="default" autowire="default"  
  116.               dependency-check="default">  
  117.               <property name="homeAddr">  
  118.                      <value>大连</value>  
  119.               </property>  
  120.        </bean>  
  121.    
  122.        <bean id="user" class="test.lyx.User" abstract="false"  
  123.               singleton="true" lazy-init="default" autowire=" byType "  
  124.               dependency-check="default">  
  125.               <property name="userName">  
  126.                      <value>liuyuanxi</value>  
  127.               </property>  
  128.               <property name="myHome">  
  129.               <ref bean="home"/>  
  130.               </property>  
  131.        </bean>  
  132. 运行时会打出   
  133. 姓名为:liuyuanxi   
  134. 家庭住址是:大连  
  135. 这种方式就是构造器注入  
  136.    
  137. 但这样的寻找方式有一个弱点,  
  138. 假如再注入一个Home bean,叫home1,运行时就会找到两个bean,出错.  
  139.        <bean id="home1" class="test.lyx.Home" abstract="false"  
  140.               singleton="true" lazy-init="default" autowire="default"  
  141.               dependency-check="default">  
  142.               <property name="homeAddr">  
  143.                      <value>beijing</value>  
  144.               </property>  
  145.        </bean>  
  146. 如果我们想解决这种问题也很简单,我们可以把autowire改为,autowire="byName"方式来寻找.  
  147. 但是这里一定要注意:Home bean的id名,一定要和属性名字一样.这里应该改成,id="myHome"  
  148.        <bean id="home1" class="test.lyx.Home" abstract="false"  
  149.               singleton="true" lazy-init="default" autowire="default"  
  150.               dependency-check="default">  
  151.               <property name="homeAddr">  
  152.                      <value>dalian</value>  
  153.               </property>  
  154.        </bean>  
  155.        <bean id="myHome" class="test.lyx.Home" abstract="false"  
  156.               singleton="true" lazy-init="default" autowire="default"  
  157.               dependency-check="default">  
  158.               <property name="homeAddr">  
  159.                      <value>北京</value>  
  160.               </property>  
  161.        </bean>  
  162. 这样的话  
  163. 运行时会打出   
  164. 姓名为:liuyuanxi   
  165. 家庭住址是:北京  
  166. 而不在是大连了,这种寻找方式就是byName,也就是按属性的名字进行查询.注意:id一定要属性的名字一样.  
  167.    
  168. 2.我们来比较一个byname和 bytype这两种方式.  
  169. byname要比bytype精确些,因为id,是不能重名的.  
  170. 而且假如存在一这样一种情况,两个user bean,分别为user1,user2,那么user1,user2都可以,打出北京.重用性很好.  
  171. 到这可能有人会想到.user1,和user2调用的是同一个Home吗.你可以把他们的hashcode();打出来,你会发现地址是一样的,也就是说,是同一个bean.这里是因为singleton="true"的原因,如果你把singleton改成了"false"那就不是一个对象了.  
  172. 3.如果把autowire=” constructor”也就是构造器注入一定要注意了,他是以byType进行查找,也就是说,Home bean,是不能出现两次,否则就会出错了.  
  173. 4.如果autowire设置成为autodetect,他会一直找,直到找到一个合适的为止.constructor,bytype,byname的顺序去找.这种方式是不推荐使用的.因为你很难判断出执行的是那个.
0 0
原创粉丝点击