該@Autowired注解提供了在哪里以及如何自動裝配應實現更細粒度的控制。@Autowired注解可用於在setter方法上自動連接bean,就像@Required注釋,構造函數,屬性或具有任意名稱和/或多個參數的方法一樣。
Setter方法中的@Autowired
您可以在setter方法上使用@Autowired注釋來擺脫XML配置文件中的<property>元素。當Spring發現使用setter方法的@Autowired注釋時,它會嘗試在方法上執行autowire="byType"的自動連接。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId> <artifactId>testannotationautowired</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>testannotationautowired</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project>
SpellChecker.java:
package com.jsoft.testspring.testannotationautowired; public class SpellChecker { public SpellChecker(){ System.out.println("SpellChecker無參數構造函數初始化"); } public void checkSpelling(){ System.out.println("SpellChecker檢查方法"); } }
TextEditor.java:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; private String name; @Autowired public void setSpellChecker(SpellChecker spellChecker){ System.out.println("TextEditor通過setter初始化"); this.spellChecker = spellChecker; } public void spellCheck() { this.spellChecker.checkSpelling(); } public void setName(String name){ this.name = name; } public void getName(){ System.out.println(this.name); } }
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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="spellChecker" class="com.jsoft.testspring.testannotationautowired.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testannotationautowired.TextEditor"> <property name="name" value="Hello World!"></property> </bean> </beans>
此時可以看見textEditor的bean上少了一個<property>標簽(元素),但是也能運行正常,因為在TextEditor類中標記了@Autowired的注解,自動匹配去匹配相同類型的bean,與在bean中設置autowire="byType"屬性保持一致。
運行結果:
而原始的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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="spellChecker" class="com.jsoft.testspring.testannotationautowired.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testannotationautowired.TextEditor"> <property name="spellChecker" ref="spellChecker"></property> <property name="name" value="Hello World!"></property> </bean> </beans>
屬性中的@Autowired
您可以在屬性上使用@Autowired注解來擺脫setter方法。當你使用<property>傳遞自動連線屬性的值時,Spring將自動為傳遞的值或引用分配這些屬性。因此,隨着@Autowired對屬性的使用,您的TextEditor.java文件將如下所示:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { @Autowired private SpellChecker spellChecker; private String name; public void spellCheck() { this.spellChecker.checkSpelling(); } public void setName(String name){ this.name = name; } public void getName(){ System.out.println(this.name); } }
可以看出上面代碼上SpellChecker的setter方法已經去掉了,而在SpellChecker的私有屬性上加入了@Autowired的注解。它會自動創建setter方法。
而beans.xml不需要改變。
運行結果如下:
構造函數中的@Autowired
您也可以將@Autowired應用於構造函數。構造函數@Autowired注解表示構造函數在創建bean時應該是自動連線的,即使在XML文件中配置bean時也不使用<constructor-arg>元素。例子如下:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; private String name; @Autowired public TextEditor(SpellChecker spellChecker){ System.out.println("TextEditor通過初始化方法賦值SpellChecker"); this.spellChecker = spellChecker; } public void spellCheck() { this.spellChecker.checkSpelling(); } public void setName(String name){ this.name = name; } public void getName(){ System.out.println(this.name); } }
而beas.xml不用改變。
測試結果:
@Autowired的(required=false)選項
默認情況下,@Autowired注解意味着依賴是必須的,它類似於@Required注解,然而,你可以使用@Autowired的(required=false)選項關閉默認行為。例子如下:
package com.jsoft.testspring.testannotationautowired; import org.springframework.beans.factory.annotation.Autowired; public class TextEditor { private SpellChecker spellChecker; private String name; @Autowired public TextEditor(SpellChecker spellChecker){ System.out.println("TextEditor通過初始化方法賦值SpellChecker"); this.spellChecker = spellChecker; } public void spellCheck() { this.spellChecker.checkSpelling(); } @Autowired(required=false) public void setName(String name){ this.name = name; } public void getName(){ System.out.println(this.name); } }
指定了SpellChecker的setter方法不是必須的,那么也修改beans.xml文件,使其不傳入此值。修改后的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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="spellChecker" class="com.jsoft.testspring.testannotationautowired.SpellChecker"></bean> <bean id="textEditor" class="com.jsoft.testspring.testannotationautowired.TextEditor"> </bean> </beans>
測試結果:
測試工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test12/testannotationautowired