在基於XML配置元數據,在bean的配置信息中咱們能夠使用<constructor-arg/>和<property/>屬性來實現Spring的依賴注入。Spring 容器也能夠在不使用<constructor-arg/>和<property/>元素下自動裝配各個bean之間的依賴關係。spring
自動裝配模式:數組
在基於XML配置元數據時,你能夠使用bean元素的autowire屬性來指定具體的自動裝配模式。Spring提供了5中自動裝配模式。app
模式 | 講解 |
no | 這是默認設置,意味着它沒有使用自動裝配模式。你應該顯示的使用bean引用來鏈接 |
byName | 由屬性名自動裝配。Spring 容器看到bean採用了自動裝配byName模式(autowire="byName"),而後根據它的屬性在Spring 容器中尋找與屬性名相同bean進行關聯 |
byType | 由屬性的數據類型自動裝配。Spring容器看到bean採用了自動裝配的byType模式(autowire="byType"),而後根據屬性類型在Spring容器中尋找與屬性類型相同bean進行關聯。若是存在不止一個這樣的bean,將拋出異常。 |
constructor | 相似於 byType,但該類型適用於構造函數參數類型。若是在容器中沒有一個構造函數參數類型的 bean,則一個致命錯誤將會發生 |
autodetect | Spring首先嚐試經過 constructor 使用自動裝配來鏈接,若是它不執行,Spring 嘗試經過 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"> <bean id="autowireTest1" class="com.test.spring.AutowireTest1" autowire="byName"> </bean> <bean id="autowireTest2" class="com.test.spring.AutowireTest2"> </bean> </beans>
Java 類:測試
package com.test.spring; public class AutowireTest1 { private AutowireTest2 autowireTest2; public AutowireTest2 getAutowireTest2() { return autowireTest2; } public void setAutowireTest2(AutowireTest2 autowireTest2) { this.autowireTest2 = autowireTest2; } } ------------------------------------------------------------------------------------------- package com.test.spring; public class AutowireTest2 { }
測試:this
package com.test.spring; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class T { ApplicationContext applicationcontext=null; @Before public void before() { System.out.println("》》》Spring ApplicationContext容器開始初始化了......"); applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"}); System.out.println("》》》Spring ApplicationContext容器初始化完畢了......"); } @Test public void test() { AutowireTest1 autowireTest1= applicationcontext.getBean(AutowireTest1.class); System.out.println(autowireTest1.getAutowireTest2()); } }
測試結果:spa
》》》Spring ApplicationContext容器開始初始化了......
2017-03-19 22:53:54 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@496419dc: startup date [Sun Mar 19 22:53:54 CST 2017]; root of context hierarchy
2017-03-19 22:53:54 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完畢了......
com.test.spring.AutowireTest2@63465272
設計
byType:3d
只須要將bean改成以下:
<bean id="autowireTest1" class="com.test.spring.AutowireTest1" autowire="byType">
測試結果:
》》》Spring ApplicationContext容器開始初始化了......
2017-03-19 22:53:54
INFO:ClassPathXmlApplicationContext-Refreshing
org.springframework.context.support.ClassPathXmlApplicationContext@496419dc:
startup date [Sun Mar 19 22:53:54 CST 2017]; root of context hierarchy
2017-03-19 22:53:54 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完畢了......
com.test.spring.AutowireTest2@63465272
constructor:
只須要將bean改成以下:
<bean id="autowireTest1" class="com.test.spring.AutowireTest1" autowire="constructor">
對應AutowireTest1類改成:
package com.test.spring; public class AutowireTest1 { private AutowireTest2 autowireTest2; public AutowireTest1(AutowireTest2 autowireTest2) { this.autowireTest2 = autowireTest2; } }
測試結果輸出:code
》》》Spring ApplicationContext容器開始初始化了......
2017-03-20 09:13:48 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4752dd7b: startup date [Mon Mar 20 09:13:48 CST 2017]; root of context hierarchy
2017-03-20 09:13:48 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完畢了......
com.test.spring.AutowireTest1@43bb703d
總結:
自動裝配的好處:
自動裝配能夠有效減小bean標籤的<constructor-arg/>或<property/>元素的使用
自動裝配的侷限性和不利條件:
1.顯示的依賴注入<constructor-arg/>或<property/>會重寫自動裝配。不能自動裝配基本數據類型、字符串、數組等,這是自動裝配設計的侷限性。
2.自動裝配不如顯示依賴注入精確。若是有可能儘可能使用顯示依賴注入。