spring?IOC容器的Bean管理XML自動(dòng)裝配過(guò)程
什么是自動(dòng)裝配?
在之前的內(nèi)容中,每給屬性注入值都要一個(gè)個(gè)的用 property 標(biāo)簽來(lái)完成,比如:
<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ī)則(屬性名稱或者屬性類型) 來(lái)自動(dòng)的將匹配的屬性值進(jìn)行注入。
自動(dòng)裝配過(guò)程
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. 測(cè)試方法
@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 0ok,到這里,其實(shí)就是手動(dòng)裝配的過(guò)程。
實(shí)現(xiàn)自動(dòng)裝配,在配置文件里,通過(guò) bean 標(biāo)簽里的屬性 autowire 來(lái)配置:
- 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í)行測(cè)試函數(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í)行測(cè)試:
Employee{department=Department{}}
Process finished with exit code 0跟使用 property 手動(dòng)裝配結(jié)果一致。
不過(guò),用 xml 方式使用自動(dòng)裝配實(shí)際中是很少的,一般是以注解的方式,后續(xù)會(huì)學(xué)習(xí)到。
以上就是spring IOC容器的Bean管理XML自動(dòng)裝配過(guò)程的詳細(xì)內(nèi)容,更多關(guān)于spring IOC Bean管理XML裝配的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot整合spring-data-redis遇到的坑
使用springboot整合redis,使用默認(rèn)的序列化配置,然后使用redis-client去查詢時(shí)查詢不到相應(yīng)的key.問(wèn)題出在哪,怎么解決呢?下面小編給大家?guī)?lái)了springboot整合spring-data-redis遇到的坑,需要的的朋友參考下吧2017-04-04
Spring 4.0新功能:@Conditional注解詳細(xì)介紹
Spring Boot的強(qiáng)大之處在于使用了Spring 4框架的新特性:@Conditional注釋,此注釋使得只有在特定條件滿足時(shí)才啟用一些配置。下面這篇文章主要給大家介紹了關(guān)于Spring4.0中新功能:@Conditional注解的相關(guān)資料,需要的朋友可以參考下。2017-09-09
SpringBoot參數(shù)校驗(yàn)之@Valid的使用詳解
這篇文章主要通過(guò)示例為大家詳細(xì)介紹一下介紹了SpringBoot參數(shù)校驗(yàn)中@Valid的使用方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
基于Jenkins自動(dòng)打包并部署docker環(huán)境的操作過(guò)程
這篇文章主要介紹了基于Jenkins自動(dòng)打包并部署docker環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08
java實(shí)現(xiàn)學(xué)生成績(jī)錄入系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生成績(jī)錄入系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

