Spring4自動裝配(default-autowire) (轉)


原文地址:http://blog.csdn.net/conglinyu/article/details/63684957

Spring 自動裝配



通過配置default-autowire 屬性,Spring IOC 容器可以自動為程序注入bean;默認是no,不啟用自動裝配;
default-autowire 的類型有byName,byType,constructor;
byName:通過名稱進行自動匹配;
byType:根據類型進行自動匹配;
constructor:和byType 類似,只不過它是根據構造方法注入而言的,根據類型,自動注入;


建議:自動裝配機制慎用,它屏蔽了裝配細節,容易產生潛在的錯誤;




【byName】

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.         default-autowire="byName">  
  7.   
  8.   
  9.     <bean id="car2" class="com.zhiqi.model.Car">  
  10.         <property name="id" value="2007"></property>  
  11.         <property name="carName" value="奧迪"></property>  
  12.     </bean>  
  13.     <!-- 自動裝配 byName 來找car -->  
  14.     <bean id="car" class="com.zhiqi.model.Car">  
  15.         <property name="id" value="2009"></property>  
  16.         <property name="carName" value="奧拓"></property>  
  17.     </bean>  
  18.   
  19.   
  20.     <bean id="employee" class="com.zhiqi.model.Employee">  
  21.         <property name="id" value="10080"></property>  
  22.         <property name="name" value="賈經理"></property>  
  23.         <property name="sex" value="男"></property>  
  24.   
  25.   
  26.     </bean>  
  27.       
  28. </beans>  



實體類

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public class Car {  
  2.   
  3.   
  4.     private int id;  
  5.     private String carName;  
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getCarName() {  
  13.         return carName;  
  14.     }  
  15.     public void setCarName(String carName) {  
  16.         this.carName = carName;  
  17.     }  
  18. }  




[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public class Employee {  
  2.   
  3.   
  4.     private int id;  
  5.     private String name;  
  6.     private String sex;  
  7.     private Car car;  
  8.   
  9.   
  10.     public Employee() {  
  11.         super();  
  12.         // TODO Auto-generated constructor stub  
  13.     }  
  14.     public Employee(int id, String name, String sex) {  
  15.         super();  
  16.         this.id = id;  
  17.         this.name = name;  
  18.         this.sex = sex;  
  19.     }  
  20.   
  21.   
  22.     public int getId() {  
  23.         return id;  
  24.     }  
  25.     public void setId(int id) {  
  26.         this.id = id;  
  27.     }  
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.     public void setName(String name) {  
  32.         this.name = name;  
  33.     }  
  34.     public String getSex() {  
  35.         return sex;  
  36.     }  
  37.     public void setSex(String sex) {  
  38.         this.sex = sex;  
  39.     }  
  40.     public Car getCar() {  
  41.         return car;  
  42.     }  
  43.     public void setCar(Car car) {  
  44.         this.car = car;  
  45.     }  
  46. }  



測試:

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyTest {  
  2.       
  3.     public static void main(String[] args) {  
  4.         ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");  
  5.         Employee employee=(Employee)ac.getBean("employee");  
  6.         //employee.setName("李經理");//在xml中屬性注入  
  7.         System.out.println(employee.getCar().getCarName());  
  8.   
  9.   
  10.     }  
  11. }  

 

 

【byType】

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.         default-autowire="byType">  
  7.   
  8.     <!-- 自動裝配 byType 有兩個相同類型會報錯 -->  
  9.     <bean id="car2" class="com.zhiqi.model.Car">  
  10.         <property name="id" value="2007"></property>  
  11.         <property name="carName" value="奧迪"></property>  
  12.     </bean>  
  13.     <!-- 自動裝配 byType 來找car -->  
  14.     <bean id="car" class="com.zhiqi.model.Car">  
  15.         <property name="id" value="2009"></property>  
  16.         <property name="carName" value="奧拓"></property>  
  17.     </bean>  
  18.   
  19.     <bean id="employee" class="com.zhiqi.model.Employee">  
  20.         <property name="id" value="10080"></property>  
  21.         <property name="name" value="賈經理"></property>  
  22.         <property name="sex" value="男"></property>  
  23.   
  24.     </bean>  
  25.       
  26. </beans>  


測試:

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. public class MyTest {  
  2.       
  3.     public static void main(String[] args) {  
  4.         ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");  
  5.         Employee employee=(Employee)ac.getBean("employee");  
  6.         //employee.setName("李經理");//在xml中屬性注入  
  7.         System.out.println(employee.getCar().getCarName());  
  8.   
  9.     }  
  10. }  


運行:

 

【default-autowire="constructor"】

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans.xsd"  
  6.         default-autowire="constructor">  
  7.   
  8.     <!-- 自動裝配 constructor 需要寫單參構造方法 -->  
  9.     <bean id="car3" class="com.zhiqi.model.Car">  
  10.         <property name="id" value="2007"></property>  
  11.         <property name="carName" value="奧迪"></property>  
  12.     </bean>  
  13.   
  14.   
  15.     <bean id="employee" class="com.zhiqi.model.Employee">  
  16.         <property name="id" value="10080"></property>  
  17.         <property name="name" value="賈經理"></property>  
  18.         <property name="sex" value="男"></property>  
  19.   
  20.     </bean>  
  21.       
  22. </beans>  

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. 自動裝配 constructor 需要寫單參構造方法  

不寫的話會報告錯誤


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM