spring?IOC容器的Bean管理XML自動(dòng)裝配過程
什么是自動(dòng)裝配?
在之前的內(nèi)容中,每給屬性注入值都要一個(gè)個(gè)的用 property 標(biāo)簽來完成,比如:
<bean id="book" class="com.pingguo.spring5.collectiontype.Book" scope="prototype"> <property name="list" ref="bookList"></property> </bean>
這就是手動(dòng)裝配。
而自動(dòng)裝配中,spring 會(huì)根據(jù)指定裝配規(guī)則(屬性名稱或者屬性類型) 來自動(dòng)的將匹配的屬性值進(jìn)行注入。
自動(dòng)裝配過程
1. 創(chuàng)建 2 個(gè)類
分別是部門類 Department 和員工類 Employee 。
package com.pingguo.spring5.autowire; public class Department { @Override public String toString() { return "Department{}"; } }
員工類有個(gè) 部門的屬性,表示員工所屬的一個(gè)部門。其他方法是為了后續(xù)方便演示輸出。
package com.pingguo.spring5.autowire; public class Employee { private Department department; public void setDepartment(Department department) { this.department = department; } @Override public String toString() { return "Employee{" + "department=" + department + '}'; } public void test() { System.out.println(department); } }
2. 配置文件
<?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="employee" class="com.pingguo.spring5.autowire.Employee"> <property name="department" ref="department"></property> </bean> <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean> </beans>
3. 測試方法
@Test public void test5() { ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml"); Employee employee = context.getBean("employee", Employee.class); System.out.println(employee); }
運(yùn)行結(jié)果:
Employee{department=Department{}} Process finished with exit code 0
ok,到這里,其實(shí)就是手動(dòng)裝配的過程。
實(shí)現(xiàn)自動(dòng)裝配,在配置文件里,通過 bean 標(biāo)簽里的屬性 autowire 來配置:
- autowire="byName":根據(jù)屬性名稱自動(dòng)注入。
- autowire="byType":根據(jù)屬性類型自動(dòng)注入。
1)byName 演示
注入值的bean的 id 值和類屬性名稱一致,比如:
修改配置文件,加上 autowire="byName",然后注釋掉 property。
<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byName"> <!--<property name="department" ref="department"></property>--> </bean> <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>
執(zhí)行測試函數(shù):
Employee{department=Department{}} Process finished with exit code 0
跟使用 property 手動(dòng)裝配結(jié)果一致。
2)byType 演示
要注入值的 bean 的類型與 屬性里的一致,比如:
現(xiàn)在繼續(xù)修改配置文件,加上 autowire="byType",然后注釋掉 property。
<bean id="employee" class="com.pingguo.spring5.autowire.Employee" autowire="byType"> <!--<property name="department" ref="department"></property>--> </bean> <bean id="department" class="com.pingguo.spring5.autowire.Department"></bean>
再次執(zhí)行測試:
Employee{department=Department{}} Process finished with exit code 0
跟使用 property 手動(dòng)裝配結(jié)果一致。
不過,用 xml 方式使用自動(dòng)裝配實(shí)際中是很少的,一般是以注解的方式,后續(xù)會(huì)學(xué)習(xí)到。
以上就是spring IOC容器的Bean管理XML自動(dòng)裝配過程的詳細(xì)內(nèi)容,更多關(guān)于spring IOC Bean管理XML裝配的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringController返回值和異常自動(dòng)包裝的問題小結(jié)
今天遇到一個(gè)需求,在不改動(dòng)原系統(tǒng)代碼的情況下,將Controller的返回值和異常包裝到一個(gè)統(tǒng)一的返回對(duì)象中去,下面通過本文給大家介紹SpringController返回值和異常自動(dòng)包裝的問題,需要的朋友可以參考下2024-03-03淺談spring和spring MVC的區(qū)別與關(guān)系
下面小編就為大家?guī)硪黄獪\談spring和spring MVC的區(qū)別與關(guān)系。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04springboot打成jar后獲取classpath下文件失敗的解決方案
這篇文章主要介紹了使用springboot打成jar后獲取classpath下文件失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Spring security如何重寫Filter實(shí)現(xiàn)json登錄
這篇文章主要介紹了Spring security 如何重寫Filter實(shí)現(xiàn)json登錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09詳解Spring ApplicationContext加載過程
這篇文章主要介紹了Spring ApplicationContext加載過程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-03-03SpringBoot中接收POST參數(shù)的幾種方式詳解
這篇文章主要介紹了SpringBoot中接收POST參數(shù)的幾種方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06