Spring 基於XML裝配Bean演練
Bean 的裝配可以理解爲依賴關係注入,Bean 的裝配方式也就是 Bean 的依賴注入方式。Spring 容器支持多種形式的 Bean 的裝配方式,如基於 XML 的 Bean 裝配、基於 Annotation 的 Bean 裝配和自動裝配等。Spring 基於 XML 的裝配通常採用兩種實現方式,即設值注入(Setter Injection)和構造注入(Constructor Injection)。
在 Spring 實例化 Bean 的過程中,首先會調用默認的構造方法實例化 Bean 對象,然後通過 Java 的反射機制調用 setXxx() 方法進行屬性的注入。因此,設值注入要求一個 Bean 的對應類必須滿足以下兩點要求:
1)必須提供一個默認的無參構造方法
2)必須爲需要注入的屬性提供對應的 setter 方法
使用設值注入時,在 Spring 配置文件中,需要使用 <bean> 元素的子元素 <property> 元素爲每個屬性注入值。而使用構造注入時,在配置文件中,主要使用 <constructor-arg> 標籤定義構造方法的參數,可以使用其 value 屬性(或子元素)設置該參數的值。下面通過案例演示基於 XML 方式的 Bean 的裝配。
1. 創建Person類
在項目 springDemo02 中的 src 目錄下,創建一個名稱爲 com.mengma.assembly 的包,在該包下創建一個 Person 類,如下所示:
package com.mengma.assembly;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// 重寫toString()方法
public String toString() {
return "Person[name=" + name + ", age=" + age + "]";
}
// 默認無參構造方法
public Person() {
super();
}
// 有參構造方法
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
上述代碼中,定義了 name 和 age 兩個屬性,併爲其提供了 getter 和 setter 方法,由於要使用構造注入,所以需要提供有參的構造方法。爲了能更清楚地看到輸出結果,這裏還重寫了 toString() 方法。
2. 創建 Spring 配置文件
在 com.mengma.assembly 包下創建一個名爲 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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<!-- 使用設值注入方式裝配Person實例 -->
<bean id="person1" class="com.mengma.assembly.Person">
<property name="name" value="zhangsan" />
<property name="age" value="20" />
</bean>
<!-- 使用構造方法裝配Person實例 -->
<bean id="person2" class="com.mengma.assembly.Person">
<constructor-arg index="0" value="lisi" />
<constructor-arg index="1" value="21" />
</bean>
</beans>
上述代碼中,首先使用了設值注入方式裝配 Person 類的實例,其中 <property> 子元素用於調用 Bean 實例中的 setXxx() 方法完成屬性賦值。然後使用了構造方式裝配了 Person 類的實例,其中 <constructor-arg> 元素用於定義構造方法的參數,其屬性 index 表示其索引(從 0 開始),value 屬性用於設置注入的值。
3. 創建測試類
在 com.mengma.assembly 包下創建一個名稱爲 XmlBeanAssemblyTest 的測試類,編輯後如下所示:
package com.mengma.assembly;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class XmlBeanAssemblyTest {
@Test
public void test() {
// 定義Spring配置文件路徑
String xmlPath = "com/mengma/assembly/applicationContext.xml";
// 初始化Spring容器,加載配置文件,並對bean進行實例化
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
// 設置方式輸出結果
System.out.println(applicationContext.getBean("person1"));
// 構造方式輸出結果
System.out.println(applicationContext.getBean("person2"));
}
}
上述代碼中,分別獲取並輸出了 id 爲 person1 和 person2 的實例。
4. 運行項目並查看結果
使用 JUnit 測試運行 XmlBeanAssemblyTest,運行成功後,控制檯的輸出結果如圖:
從輸出結果中可以看出,使用設值注入和構造注入兩種方式都成功裝配了 Person 實例。