Bean注入管理方式成為bean的裝配政策。
IoC容器可以自動裝配autowire互相協作bean之間的關聯關系。可以讓BeanFactory中的内容來替代我們指定的bean的協作者。
使用自動裝配可以使配置檔案減少。
為了是大家能更好的的了解自動裝配的原理,我用代碼向大家,解釋效果能更為直覺。
1.在不使用自動裝配的情況下。使用p命名空間
首先定義兩個類。
類B1
public class B1 {
}
類A1使用B1這個對象
public class A1 {
private B1 b1;
public A1(){}
public A1(B1 b1){
System.out.println("A1的構造器");
this.b1 = b1;
}
public void pp(){
System.out.println("this is A1.class");
System.out.println("調用B的方法"+b1);
}
public B1 getB1() {
System.out.println("get方法");
return b1;
}
public void setB1(B1 b1) {
System.out.println("set方法");
this.b1 = b1;
}
}
配置檔案
使用p名空間
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-autowire="constructor">
<bean id="aa" class="com.jia.A2.A1" p:bb-ref="bb"/>
<bean id="bb" class="com.jia.A2.B1"/>
</beans>
測試上述檔案代碼
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
A1 a1 = applicationContext.getBean("aa",A1.class);
a1.pp();
}
}
測試結果
A1的構造器
set方法
this is A1.class
調用B的方法com.jia.A2.B1@1a9ff430
在使用autowire的情況下。
autowire的五種屬性解析。
-
byType
按照屬性類型進行自動裝配[使用的是設定器注入],如果A類中有個類型為B的屬性,如果受管bean中有對應類型的對象,則容器自動調用set方法設定A對象的這個屬性,不管名稱是否對應
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-autowire="byType">
<bean id="aa" class="com.jia.A2.A1" autowire="default"/>
<bean id="bb" class="com.jia.A2.B1"/>
</beans>
運作結果
set方法
this is A1.class
調用B的方法com.jia.A2.B1@7e2b2718
2.byName
意思是采用名稱自動裝配[使用的是設定器注入],A中有個屬性叫b,如果目前配置中有一個受管bean名稱為b,則系統自動調用set方法将其進行注入到A對象中
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-autowire="byType">
<bean id="aa" class="com.jia.A2.A1" autowire="byName"/>
<bean id="bb" class="com.jia.A2.B1"/>
</beans>
運作結果
this is A1.class
調用B的方法null
//沒調取到B1的方法是因為A中屬性叫b1,而配置中的受管bean名稱為bb是以自動裝配無法識别
3.contructor
按照構造器進行自動裝配
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-autowire="byType">
<bean id="aa" class="com.jia.A2.A1" autowire="contructor"/>
<bean id="bb" class="com.jia.A2.B1"/>
</beans>
運作結果
A1的構造器
this is A1.class
調用B的方法com.jia.A2.B1@67e9d110
4.default
預設自動裝配
在配置檔案的根标簽上有一個配置屬性
default-autowire=”no/byType/byName/contructor”可以定義目前配置檔案中的預設裝配方法
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
default-autowire="byType">
<bean id="aa" class="com.jia.A2.A1" autowire="default"/>
<bean id="bb" class="com.jia.A2.B1"/>
</beans>
運作結果
set方法
this is A1.class
調用B的方法com.jia.A2.B1@7e2b2718
//這裡我使用預設的自動裝配,而自動裝配是byType,調用了set方法
4.no
預設不使用自動裝配
一般情況下,在實際的項目中很少使用自動裝配功能,因為和自動裝配功能所帶來的好處比起來,明确清晰的配置文檔更有說服力一些