Spring 自動裝配使用byType
模式規定由自動裝配屬性類型。Spring容器在外觀上autowire屬性設置為byType的XML配置文件中的bean。然後,它嘗試匹配和連接一個屬性,如果它的類型有完全相同的豆子名稱的一個匹配的配置文件。如果找到匹配項,它會注入這些bean,否則,它會拋出異常。
例如,如果一個bean定義設置為自動裝配byType的配置文件,它包含拚寫檢查類型的aspellChecker屬性,春季查找名為拚寫檢查一個bean定義,並用它來設置該屬性。仍然可以使用<property>標簽接線其餘屬性。下麵的例子將說明這個概念,會發現和上麵的例子冇有什麼區彆,除了XML配置文件已被更改。
讓我們使用Eclipse IDE,然後按照下麵的步驟來創建一個Spring應用程序:
步驟 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes TextEditor, SpellChecker and MainApp under the com.yiibaipackage. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
這裡是TextEditor.java文件的內容:
package com.yiibai; 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(); } }
下麵是另外一個相關的類文件SpellChecker.java內容:
package com.yiibai; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的內容:
package com.yiibai; 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.yiibai.TextEditor"> <property name="spellChecker" ref="spellChecker" /> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
但是,如果要使用自動裝配“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.yiibai.TextEditor" autowire="byType"> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="SpellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
當創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,這將打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.