Spring @Autowired注解
@Autowired在Setter方法:
您可以使用@Autowired注解放在setter方法來擺脫在XML配置文件中的<property>元素。當Spring發現setter方法使用@ Autowired注解,它會嘗試對方法進行byType的自動裝配。
例子
我們使用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; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; @Autowired public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker( ) { return spellChecker; } 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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg --> <bean id="textEditor" class="com.yiibai.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,將打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.
@Autowired 在 Properties:
您可以使用@Autowired注解的屬性擺脫setter方法。當使用<property>通過自動裝配屬性的值Spring會自動分配這些屬性與傳遞的值或引用。因此,@Autowired上的屬性的使用TextEditor.javafile將如下:
package com.yiibai; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { @Autowired private SpellChecker spellChecker; public TextEditor() { System.out.println("Inside TextEditor constructor." ); } public SpellChecker getSpellChecker( ){ return spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); } }
以下是配置文件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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,將打印以下信息:
Inside TextEditor constructor. Inside SpellChecker constructor. Inside checkSpelling.
@Autowired 在構造函數:
可以將@Autowired在構造函數中。構造函數和@Autowired注解表明該構造函數應該在創建這個bean時,自動裝配,即使在配置bean的XML文件冇有<constructor-arg>元素被使用。讓我們檢查下麵的例子。
這裡是TextEditor.java文件的內容:
package com.yiibai; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; @Autowired public TextEditor(SpellChecker spellChecker){ System.out.println("Inside TextEditor constructor." ); this.spellChecker = spellChecker; } public void spellCheck(){ spellChecker.checkSpelling(); } }
以下是配置文件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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/> <!-- Definition for textEditor bean without constructor-arg --> <bean id="textEditor" class="com.yiibai.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,將打印以下信息:
Inside TextEditor constructor. Inside SpellChecker constructor. Inside checkSpelling.
@Autowired 與 (required=false) 選項
默認情況下,@ Autowired注解意味著依賴關係需要類似@ Required注解,但是,你可以通過使用(required=false)選項使用@Autowired的關閉的默認行為。
下麵的例子將工作,即使你冇有通過age屬性的任何值,但它仍然會為name屬性的要求。你自己可以試試這個例子,因為這是使用@Required注解例子不同的是僅Student.java文件已被更改。
package com.yiibai; import org.springframework.beans.factory.annotation.Autowired; public class Student { private Integer age; private String name; @Autowired(required=false) public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age; } @Autowired public void setName(String name) { this.name = name; } public String getName() { return name; } }