Spring由構造函數自動裝配
這種模式是非常相似byType,但它應用於構造器參數。 Spring容器在外觀上autowire屬性被設置XML配置文件中bean的。然後,它嘗試匹配和連線它的構造函數的參數與bean名稱的配置文件隻有一個。如果找到匹配項,它會注入這些bean,否則,它會拋出異常。
例如,如果一個bean定義設置為通過構造配置文件自動裝配,它具有與拚寫檢查類型的參數之一的構造函數,春天尋找一個bean定義namedSpellChecker,並用它來設置構造函數的參數。仍然可以使用<constructor-arg>標簽連線剩餘的參數。下麵的例子將說明這個概念。
讓我們使用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 TextEditor( SpellChecker spellChecker, String name ) { this.spellChecker = spellChecker; this.name = name; } public SpellChecker getSpellChecker() { return spellChecker; } 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"> <constructor-arg ref="spellChecker" /> <constructor-arg value="Generic Text Editor"/> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </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" autowire="constructor"> <constructor-arg value="Generic Text Editor"/> </bean> <!-- Definition for spellChecker bean --> <bean id="SpellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,這將打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.