spring?IOC容器的Bean管理XML自動裝配過程
什么是自動裝配?
在之前的內(nèi)容中,每給屬性注入值都要一個個的用 property 標簽來完成,比如:
<bean id="book" class="com.pingguo.spring5.collectiontype.Book" scope="prototype">
<property name="list" ref="bookList"></property>
</bean>這就是手動裝配。
而自動裝配中,spring 會根據(jù)指定裝配規(guī)則(屬性名稱或者屬性類型) 來自動的將匹配的屬性值進行注入。
自動裝配過程
1. 創(chuàng)建 2 個類
分別是部門類 Department 和員工類 Employee 。
package com.pingguo.spring5.autowire;
public class Department {
@Override
public String toString() {
return "Department{}";
}
}員工類有個 部門的屬性,表示員工所屬的一個部門。其他方法是為了后續(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);
}運行結果:
Employee{department=Department{}}
Process finished with exit code 0ok,到這里,其實就是手動裝配的過程。
實現(xiàn)自動裝配,在配置文件里,通過 bean 標簽里的屬性 autowire 來配置:
- autowire="byName":根據(jù)屬性名稱自動注入。
- autowire="byType":根據(jù)屬性類型自動注入。
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 手動裝配結果一致。
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 手動裝配結果一致。
不過,用 xml 方式使用自動裝配實際中是很少的,一般是以注解的方式,后續(xù)會學習到。
以上就是spring IOC容器的Bean管理XML自動裝配過程的詳細內(nèi)容,更多關于spring IOC Bean管理XML裝配的資料請關注腳本之家其它相關文章!
相關文章
springboot整合spring-data-redis遇到的坑
使用springboot整合redis,使用默認的序列化配置,然后使用redis-client去查詢時查詢不到相應的key.問題出在哪,怎么解決呢?下面小編給大家?guī)砹藄pringboot整合spring-data-redis遇到的坑,需要的的朋友參考下吧2017-04-04
Spring 4.0新功能:@Conditional注解詳細介紹
Spring Boot的強大之處在于使用了Spring 4框架的新特性:@Conditional注釋,此注釋使得只有在特定條件滿足時才啟用一些配置。下面這篇文章主要給大家介紹了關于Spring4.0中新功能:@Conditional注解的相關資料,需要的朋友可以參考下。2017-09-09
SpringBoot參數(shù)校驗之@Valid的使用詳解
這篇文章主要通過示例為大家詳細介紹一下介紹了SpringBoot參數(shù)校驗中@Valid的使用方法,文中的示例代碼講解詳細,需要的可以參考一下2022-06-06
基于Jenkins自動打包并部署docker環(huán)境的操作過程
這篇文章主要介紹了基于Jenkins自動打包并部署docker環(huán)境,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08

