Spring的自動裝配Bean
阿新 • • 發佈:2019-02-12
Spring的自動裝配功能的定義:無須在Spring配置檔案中描述javaBean之間的依賴關係(如配置<property>、<constructor-arg>)。IOC容器會自動建立javabean之間的關聯關係。
如果沒有采用自動裝配的話,手動裝配我們通常在配置檔案中進行實現:一下程式碼就是手動裝配:
<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-3.0.xsd"> <bean id="customerDAO" class="com.hebeu.customer.dao.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
通過<property name="dataSource" ref="dataSource" />向customerDAO的bean中注入了dataSource 在Spring框架,可以用 auto-wiring 功能會自動裝配Bean。要啟用它,只需要在 <bean>定義“autowire”屬性。
<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />在Spring中,支援 5 自動裝配模式。
- no – 預設情況下,自動配置是通過“ref”屬性手動設定
- byName – 根據屬性名稱自動裝配。如果一個bean的名稱和其他bean屬性的名稱是一樣的,將會自裝配它。
- byType – 按資料型別自動裝配。如果一個bean的資料型別是用其它bean屬性的資料型別,相容並自動裝配它。
- constructor – 在建構函式引數的byType方式。
- autodetect – 如果找到預設的建構函式,使用“自動裝配用構造”; 否則,使用“按型別自動裝配”。【在Spring3.0以後的版本被廢棄,已經不再合法了】
-
第一種自動裝配【根據屬性名稱自動裝配】
package com.hebeu.model; public class Customer { private Address address; public Customer() { } public Customer(int id, Address address) { super(); this.address = address; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
package com.hebeu.model;
public class Address {
private String fulladdress;
public Address(){
}
public Address(String addr){
this.fulladdress = addr;
}
public String getFulladdress() {
return fulladdress;
}
public void setFulladdress(String fulladdress) {
this.fulladdress = fulladdress;
}
}
<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-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer" autowire="byName"></bean>
<bean id="address" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
這樣就將address注入到Customer中。這就是自動注入ByName.在Customer bean中公開了一個屬性address,Spring容器會找到address bean,並且裝配。這裡必須要注意,Customer中要被注入的bean的set方法要求必須是public的,否則會報空指標異常。還有配置的bean的id必須和Customer中宣告的變數名相同。
第二種自動裝配【根據資料型別自動裝配】
宣告的倆個bean同樣為Customer以及Address,將applicationContext.xml轉換為這樣的就是實現根據資料型別進行自動裝配。
<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-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean>
<bean id="bean1" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
型別自動裝配的意思是如果一個bean的資料型別與其他的bean屬性的資料型別相同,將會自動相容裝配它。當然要求只能配置一個某一個型別的bean.如果配置成這樣,那麼是會出錯的。
<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-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean>
<bean id="bean1" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
<bean id="bean2" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
第三種自動裝配【根據構造方法自動裝配】
案例同上,將applicationContext.xml轉換為如下,就實現了按照構造方法自動裝配:
<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-3.0.xsd">
<bean id="customer" class="com.hebeu.model.Customer">
<!-- 構造方法的引數 -->
<constructor-arg>
<ref bean="bean1"/>
</constructor-arg>
</bean>
<bean id="bean1" class="com.hebeu.model.Address">
<property name="fulladdress" value="YiLong Road, CA 188"></property>
</bean>
</beans>
它實際上是建構函式的引數型別自動裝配。這意味著如果一個bean的資料型別與其他bean的構造器引數的資料型別是相同的,那麼就自動裝配。