原文地址: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】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byName">
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奧迪"></property>
- </bean>
- <!-- 自動裝配 byName 來找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奧拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="賈經理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
實體類
- public class Car {
- private int id;
- private String carName;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getCarName() {
- return carName;
- }
- public void setCarName(String carName) {
- this.carName = carName;
- }
- }
- public class Employee {
- private int id;
- private String name;
- private String sex;
- private Car car;
- public Employee() {
- super();
- // TODO Auto-generated constructor stub
- }
- public Employee(int id, String name, String sex) {
- super();
- this.id = id;
- this.name = name;
- this.sex = sex;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public Car getCar() {
- return car;
- }
- public void setCar(Car car) {
- this.car = car;
- }
- }
測試:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李經理");//在xml中屬性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
【byType】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="byType">
- <!-- 自動裝配 byType 有兩個相同類型會報錯 -->
- <bean id="car2" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奧迪"></property>
- </bean>
- <!-- 自動裝配 byType 來找car -->
- <bean id="car" class="com.zhiqi.model.Car">
- <property name="id" value="2009"></property>
- <property name="carName" value="奧拓"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="賈經理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
測試:
- public class MyTest {
- public static void main(String[] args) {
- ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
- Employee employee=(Employee)ac.getBean("employee");
- //employee.setName("李經理");//在xml中屬性注入
- System.out.println(employee.getCar().getCarName());
- }
- }
運行:
【default-autowire="constructor"】
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- default-autowire="constructor">
- <!-- 自動裝配 constructor 需要寫單參構造方法 -->
- <bean id="car3" class="com.zhiqi.model.Car">
- <property name="id" value="2007"></property>
- <property name="carName" value="奧迪"></property>
- </bean>
- <bean id="employee" class="com.zhiqi.model.Employee">
- <property name="id" value="10080"></property>
- <property name="name" value="賈經理"></property>
- <property name="sex" value="男"></property>
- </bean>
- </beans>
- 自動裝配 constructor 需要寫單參構造方法
不寫的話會報告錯誤