Spring自動(dòng)裝配Bean實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了Spring自動(dòng)裝配Bean實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
要使用自動(dòng)裝配,就需要配置 <bean> 元素的 autowire 屬性。autowire 屬性有五個(gè)值,具體說(shuō)明如表 1 所示。
表 1 autowire 的屬性和作用
名稱 | 說(shuō)明 |
---|---|
byName | 根據(jù) Property 的 name 自動(dòng)裝配,如果一個(gè) Bean 的 name 和另一個(gè) Bean 中的 Property 的 name 相同,則自動(dòng)裝配這個(gè) Bean 到 Property 中。 |
byType | 根據(jù) Property 的數(shù)據(jù)類型(Type)自動(dòng)裝配,如果一個(gè) Bean 的數(shù)據(jù)類型兼容另一個(gè) Bean 中 Property 的數(shù)據(jù)類型,則自動(dòng)裝配。 |
constructor | 根據(jù)構(gòu)造方法的參數(shù)的數(shù)據(jù)類型,進(jìn)行 byType 模式的自動(dòng)裝配。 |
autodetect | 如果發(fā)現(xiàn)默認(rèn)的構(gòu)造方法,則用 constructor 模式,否則用 byType 模式。 |
no | 默認(rèn)情況下,不使用自動(dòng)裝配,Bean 依賴必須通過(guò) ref 元素定義。 |
下面通過(guò)案例演示如何實(shí)現(xiàn)自動(dòng)裝配。首先將 applicationContext.xml 配置文件修改成自動(dòng)裝配形式,如下所示。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="personDao" class="com.mengma.annotation.PersonDaoImpl" /> <bean id="personService" class="com.mengma.annotation.PersonServiceImpl" autowire="byName" /> <bean id="personAction" class="com.mengma.annotation.PersonAction" autowire="byName" /> </beans>
在上述配置文件中,用于配置 personService 和 personAction 的 <bean> 元素中除了 id 和 class 屬性以外,還增加了 autowire 屬性,并將其屬性值設(shè)置為 byName(按屬性名稱自動(dòng)裝配)。
默認(rèn)情況下,配置文件中需要通過(guò) ref 裝配 Bean,但設(shè)置了 autowire="byName",Spring 會(huì)在配置文件中自動(dòng)尋找與屬性名字 personDao 相同的 <bean>,找到后,通過(guò)調(diào)用 setPersonDao(PersonDao personDao)方法將 id 為 personDao 的 Bean 注入 id 為 personService 的 Bean 中,這時(shí)就不需要通過(guò) ref 裝配了。
使用 JUnit 再次運(yùn)行測(cè)試類中的 test() 方法,控制臺(tái)的顯示結(jié)果如圖所示。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot lua檢查redis庫(kù)存的實(shí)現(xiàn)示例
本文主要介紹了springboot lua檢查redis庫(kù)存的實(shí)現(xiàn)示例,為了優(yōu)化性能,通過(guò)Lua腳本實(shí)現(xiàn)對(duì)多個(gè)馬戲場(chǎng)次下的座位等席的庫(kù)存余量檢查,感興趣的可以了解一下2024-09-09Springboot使用RestTemplate調(diào)用第三方接口的操作代碼
這篇文章主要介紹了Springboot使用RestTemplate調(diào)用第三方接口,我只演示了最常使用的請(qǐng)求方式get、post的簡(jiǎn)單使用方法,當(dāng)然RestTemplate的功能還有很多,感興趣的朋友可以參考RestTemplate源碼2022-12-12Spring循環(huán)依賴實(shí)現(xiàn)過(guò)程揭秘
這篇文章主要介紹了Spring循環(huán)依賴實(shí)現(xiàn)過(guò)程,Spring的解決循環(huán)依賴是有前置條件的,要解決循環(huán)依賴我們首先要了解Spring Bean對(duì)象的創(chuàng)建過(guò)程和依賴注入的方式2023-01-01SpringBoot如何使用內(nèi)嵌Tomcat問(wèn)題
這篇文章主要介紹了SpringBoot如何使用內(nèi)嵌Tomcat問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06SpringBoot結(jié)合ProGuard實(shí)現(xiàn)代碼混淆(最新版)
這篇文章主要介紹了SpringBoot結(jié)合ProGuard實(shí)現(xiàn)代碼混淆(最新版),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10spring boot 加載web容器tomcat流程源碼分析
本文章主要描述spring boot加載web容器 tomcat的部分,為了避免文章知識(shí)點(diǎn)過(guò)于分散,其他相關(guān)的如bean的加載,tomcat內(nèi)部流程等不做深入討論,具體內(nèi)容詳情跟隨小編一起看看吧2021-06-06Java類的定義以及執(zhí)行順序?qū)W習(xí)教程
這篇文章主要介紹了Java類的定義以及執(zhí)行順序?qū)W習(xí)教程,包括對(duì)象的創(chuàng)建等面向?qū)ο缶幊痰幕A(chǔ)知識(shí),需要的朋友可以參考下2015-09-09