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 需要写单参构造方法  

不写的话会报告错误

原文地址:https://www.cnblogs.com/zhaotiancheng/p/6637125.html