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