Spring的自動(dòng)裝配Bean的三種方式
spring的自動(dòng)裝配功能的定義:無(wú)須在Spring配置文件中描述javaBean之間的依賴關(guān)系(如配置<property>、<constructor-arg>)。IOC容器會(huì)自動(dòng)建立javabean之間的關(guān)聯(lián)關(guān)系。
如果沒(méi)有采用自動(dòng)裝配的話,手動(dòng)裝配我們通常在配置文件中進(jìn)行實(shí)現(xiàn):一下代碼就是手動(dòng)裝配:
<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>
通過(guò)<property name="dataSource" ref="dataSource" />向customerDAO的bean中注入了dataSource
在Spring框架,可以用 auto-wiring 功能會(huì)自動(dòng)裝配Bean。要啟用它,只需要在 <bean>定義“autowire”屬性。
<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />
在Spring中,支持 5 自動(dòng)裝配模式。
- no – 缺省情況下,自動(dòng)配置是通過(guò)“ref”屬性手動(dòng)設(shè)定
- byName – 根據(jù)屬性名稱自動(dòng)裝配。如果一個(gè)bean的名稱和其他bean屬性的名稱是一樣的,將會(huì)自裝配它。
- byType – 按數(shù)據(jù)類型自動(dòng)裝配。如果一個(gè)bean的數(shù)據(jù)類型是用其它bean屬性的數(shù)據(jù)類型,兼容并自動(dòng)裝配它。
- constructor – 在構(gòu)造函數(shù)參數(shù)的byType方式。
- autodetect – 如果找到默認(rèn)的構(gòu)造函數(shù),使用“自動(dòng)裝配用構(gòu)造”; 否則,使用“按類型自動(dòng)裝配”?!驹赟pring3.0以后的版本被廢棄,已經(jīng)不再合法了】
第一種自動(dòng)裝配【根據(jù)屬性名稱自動(dòng)裝配】
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中。這就是自動(dòng)注入ByName.在Customer bean中公開了一個(gè)屬性address,Spring容器會(huì)找到address bean,并且裝配。這里必須要注意,Customer中要被注入的bean的set方法要求必須是public的,否則會(huì)報(bào)空指針異常。還有配置的bean的id必須和Customer中聲明的變量名相同。
第二種自動(dòng)裝配【根據(jù)數(shù)據(jù)類型自動(dòng)裝配】
聲明的倆個(gè)bean同樣為Customer以及Address,將applicationContext.xml轉(zhuǎn)換為這樣的就是實(shí)現(xiàn)根據(jù)數(shù)據(jù)類型進(jìn)行自動(dòng)裝配。
<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" <strong><span style="color:#FF0000;">autowire="byType"</span></strong>></bean> <bean id="bean1" class="com.hebeu.model.Address"> <property name="fulladdress" value="YiLong Road, CA 188"></property> </bean> </beans>
類型自動(dòng)裝配的意思是如果一個(gè)bean的數(shù)據(jù)類型與其他的bean屬性的數(shù)據(jù)類型相同,將會(huì)自動(dòng)兼容裝配它。當(dāng)然要求只能配置一個(gè)某一個(gè)類型的bean.如果配置成這樣,那么是會(huì)出錯(cuò)的。
<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>
第三種自動(dòng)裝配【根據(jù)構(gòu)造方法自動(dòng)裝配】
案例同上,將applicationContext.xml轉(zhuǎn)換為如下,就實(shí)現(xiàn)了按照構(gòu)造方法自動(dòng)裝配:
<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"> <!-- 構(gòu)造方法的參數(shù) --> <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>
它實(shí)際上是構(gòu)造函數(shù)的參數(shù)類型自動(dòng)裝配。這意味著如果一個(gè)bean的數(shù)據(jù)類型與其他bean的構(gòu)造器參數(shù)的數(shù)據(jù)類型是相同的,那么就自動(dòng)裝配。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java WebSocket的實(shí)現(xiàn)以及Spring WebSocket示例代碼
本篇文章主要介紹了java WebSocket的實(shí)現(xiàn)以及Spring WebSocket,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01idea2023創(chuàng)建JavaWeb教程之右鍵沒(méi)有Servlet的問(wèn)題解決
最近在寫一個(gè)javaweb項(xiàng)目,但是在IDEA中創(chuàng)建好項(xiàng)目后,在搭建結(jié)構(gòu)的時(shí)候創(chuàng)建servlet文件去沒(méi)有選項(xiàng),所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于idea2023創(chuàng)建JavaWeb教程之右鍵沒(méi)有Servlet問(wèn)題的解決方法,需要的朋友可以參考下2023-10-10java web監(jiān)聽器統(tǒng)計(jì)在線用戶及人數(shù)
本文主要介紹了java web監(jiān)聽器統(tǒng)計(jì)在線用戶及人數(shù)的方法解析。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04多線程Thread,Runnable,Callable實(shí)現(xiàn)方式
這篇文章主要為大家詳細(xì)介紹了Java多線程如何實(shí)現(xiàn)Thread,Runnable,Callable的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Spring Boot 如何自定義返回錯(cuò)誤碼錯(cuò)誤信息
這篇文章主要介紹了Spring Boot 如何自定義返回錯(cuò)誤碼錯(cuò)誤信息的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Java調(diào)用騰訊云短信API接口的實(shí)現(xiàn)
這篇文章主要介紹了Java調(diào)用騰訊云短信API接口的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07解讀動(dòng)態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問(wèn)題
這篇文章主要介紹了解讀動(dòng)態(tài)數(shù)據(jù)源dynamic-datasource-spring-boot-starter使用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03