Spring裝配Bean(基於XML)
通過XML裝配Bean
在Spring剛剛出現的時候,XML是描述配置的主要配置。隨著Spring的發展,自動化裝配和基於Java的配置成為了主流,但理解XML配置Bean是很有必要的,因為你有可能要維護已有的XML配置。
- 在XML中宣告一個簡單的Bean。
在這裡,我們用學生讀書的例子,先寫一個Book介面。
package XMLConfig;
public interface Book {
void say();
}
然後再寫一個數學書的實現類。
package XMLConfig; public class MathBook implements Book { @Override public void say() { System.out.println("我是數學書。"); } }
最後在spring-config.xml配置檔案中宣告一個Bean。
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="mathBook" class="XMLConfig.MathBook"/> </beans>
宣告一個Bean就是這麼簡單,只需指定Bean標籤的id和class屬性即可。
2.藉助構造器注入初始化Bean。
在這裡寫People介面和Student實現類。
package XMLConfig;
public interface People {
void seeBook();
}
package XMLConfig; public class Student implements People { private Book book; public Student(Book book) { this.book=book; } @Override public void seeBook() { book.say(); } }
在第二段程式碼可以看到Student類需要通過建構函式注入Book類。
通過構造器注入,有兩種方案可供選擇。
-
<constructor-arg>元素
-
使用Spring 3.0所引入的c-名稱空間
2.1 使用<constructor-arg>元素。
<bean id="student" class="XMLConfig.Student">
<constructor-arg ref="mathBook"/>
</bean>
2.2 使用Spring 3.0所引入的c-名稱空間。
使用c-名稱空間,必須在XML的頭部加上
xmlns:c="http://www.springframework.org/schema/c"。
<bean id="student" class="XMLConfig.Student" c:book-ref="mathBook"/>
2.3將字面量注入到構造器中。
有可能我們的類所需要的屬性只是簡單的字串,例如下面的類。
package XMLConfig;
public class Man {
private String name;
private String hobby;
public Man(String name,String hobby){
this.name=name;
this.hobby=hobby;
}
public void introduce(){
System.out.println("我的名字叫"+name+",我的愛好是"+hobby);
}
}
這是我們使用<constructor arg value=""/>進行注入。
<bean id="man" class="XMLConfig.Man">
<constructor-arg value="tom"/>
<constructor-arg value="讀書"/>
</bean>
2.4裝配集合。
有可能Man類的愛好有很多,這裡我們用List集合進行表達。
package XMLConfig;
import java.util.List;
public class Man {
private String name;
private List<String> hobbys;
public Man(String name,List<String> hobbys){
this.name=name;
this.hobbys=hobbys;
}
public void introduce(){
String intr="我的名字叫"+name+",我喜歡";
for (String hobby:hobbys){
intr=intr+hobby+",";
}
System.out.println(intr);
}
}
這時候我們的XML配置檔案可以這樣寫。
<bean id="man" class="XMLConfig.Man">
<constructor-arg value="tom"/>
<constructor-arg>
<list>
<value>讀書</value>
<value>健身</value>
<value>看動漫</value>
</list>
</constructor-arg>
</bean>
3.通過setter方法
3.1注入其他的Bean。
package XMLConfig;
public class Student implements People {
private Book book;
public void setBook(Book book) {
this.book = book;
}
@Override
public void seeBook() {
book.say();
}
}
在上面的程式碼可以看出,Student類已經沒有了注入屬性的構造方法,只有相應的setter方法。這時候我們使用<property name="被注入的屬性" ref="引用的屬性">進行配置。配置資訊如下。
<bean id="student" class="XMLConfig.Student">
<property name="book" ref="mathBook"/>
</bean>
3.2注入字面量,如字串。這裡我們使用<property name="被注入的屬性" value="要注入的字面量">,例如。
<bean id="man" class="XMLConfig.Man">
<property name="name" value="tom"/>
</bean>
3.3注入List集合。語法和上述差不多。
<bean id="man" class="XMLConfig.Man">
<property name="name" value="tom"/>
<property name="hobbys">
<list>
<value>讀書</value>
<value>睡覺</value>
<value>看動漫</value>
</list>
</property>
</bean>
4.利用junit進行測試。
JUnit是一個Java語言的單元測試框架,有了它,我們可以在一個普通的Java類中進行Spring Bean的測試。要使用Junit,我們需要匯入以下兩個jar包。
具體的用法如下。我們在XML中已經聲明瞭student的Bean,在這裡裝配到people,並在test呼叫people的sessBook()方法。
package XMLConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/spring-config.xml")
public class test {
@Autowired
private People people;
@Test
public void test(){
people.seeBook();
}
}
執行結果如下:
從上面可以看出在XML中的確成功的配置了Bean。