這種模式由屬性類型指定自動(dòng)裝配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 屬性設(shè)置為 byType。然后,如果它的 type 恰好與配置文件中 beans 名稱中的一個(gè)相匹配,它將嘗試匹配和連接它的屬性。如果找到匹配項(xiàng),它將注入這些 beans,否則,它將拋出異常。
例如,在配置文件中,如果一個(gè) bean 定義設(shè)置為自動(dòng)裝配 byType,并且它包含 SpellChecker 類型的 spellChecker 屬性,那么 Spring 就會(huì)查找定義名為 SpellChecker 的 bean,并且用它來設(shè)置這個(gè)屬性。你仍然可以使用 <property> 標(biāo)簽連接其余屬性。下面的例子將說明這個(gè)概念,你會(huì)發(fā)現(xiàn)和上面的例子沒有什么區(qū)別,除了 XML 配置文件已經(jīng)被改變。
讓我們在恰當(dāng)?shù)奈恢檬褂?Eclipse IDE,然后按照下面的步驟來創(chuàng)建一個(gè) Spring 應(yīng)用程序:
步驟 | 描述 |
---|---|
1 | 創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目,并且在已創(chuàng)建的項(xiàng)目的 src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint。 |
2 | 使用 Add External JARs 選項(xiàng),添加所需的 Spring 庫,在 Spring Hello World Example 章節(jié)中已說明。 |
3 | 在 com.tutorialspoint 包中創(chuàng)建 Java 類 TextEditor,SpellChecker 和 MainApp。 |
4 | 在 src 文件夾中創(chuàng)建 Beans 的配置文件 Beans.xml。 |
5 | 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并運(yùn)行該應(yīng)用程序,正如下面解釋的一樣。 |
這里是 TextEditor.java 文件的內(nèi)容:
package com.tutorialspoint;
public class TextEditor {
private SpellChecker spellChecker;
private String name;
public void setSpellChecker( SpellChecker spellChecker ) {
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
下面是另一個(gè)依賴類文件 SpellChecker.java 的內(nèi)容:
package com.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
下面是 MainApp.java 文件的內(nèi)容:
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
下面是在正常情況下的配置文件 Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker" />
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
但是,如果你要使用自動(dòng)裝配 “byType”,那么你的 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor"
autowire="byType">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
一旦你完成了創(chuàng)建源代碼和 bean 的配置文件,我們就可以運(yùn)行該應(yīng)用程序。如果你的應(yīng)用程序一切都正常,它將打印下面的消息:
Inside SpellChecker constructor.
Inside checkSpelling.