@Autowired 注釋對在哪里和如何完成自動連接提供了更多的細微的控制。
@Autowired 注釋可以在 setter 方法中被用于自動連接 bean,就像 @Autowired 注釋,容器,一個屬性或者任意命名的可能帶有多個參數的方法。
你可以在 XML 文件中的 setter 方法中使用 @Autowired 注釋來除去 元素。當 Spring遇到一個在 setter 方法中使用的 @Autowired 注釋,它會在方法中視圖執行 byType 自動連接。
讓我們使 Eclipse IDE 處于工作狀態,然后按照如下步驟創建一個 Spring 應用程序:
步驟 | 描述 |
---|---|
1 | 創建一個名為 SpringExample 的項目,并且在所創建項目的 src 文件夾下創建一個名為 com.tutorialspoint 的包。 |
2 | 使用 Add External JARs 選項添加所需的 Spring 庫文件,就如在 Spring Hello World Example 章節中解釋的那樣。 |
3 | 在 com.tutorialspoint 包下創建 Java 類 TextEditor, SpellChecker 和 MainApp。 |
4 | 在 src 文件夾下創建 Beans 配置文件 Beans.xml。 |
5 | 最后一步是創建所有 Java 文件和 Bean 配置文件的內容,并且按如下解釋的那樣運行應用程序。 |
這里是 TextEditor.java 文件的內容:
package com.tutorialspoint;
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.tutorialspoint;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}
下面是 MainApp.java 文件的內容:
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" 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.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
一旦你已經完成的創建了源文件和 bean 配置文件,讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將會輸出以下消息:
Inside SpellChecker constructor.
Inside checkSpelling.
你可以在屬性中使用 @Autowired 注釋來除去 setter 方法。當時使用 為自動連接屬性傳遞的時候,Spring 會將這些傳遞過來的值或者引用自動分配給那些屬性。所以利用在屬性中 @Autowired 的用法,你的 TextEditor.java 文件將變成如下所示:
package com.tutorialspoint;
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.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
一旦你在源文件和 bean 配置文件中完成了上面兩處改變,讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將會輸出以下消息:
Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.
你也可以在構造函數中使用 @Autowired。一個構造函數 @Autowired 說明當創建 bean 時,即使在 XML 文件中沒有使用 元素配置 bean ,構造函數也會被自動連接。讓我們檢查一下下面的示例。
這里是 TextEditor.java 文件的內容:
package com.tutorialspoint;
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.tutorialspoint.TextEditor"> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.tutorialspoint.SpellChecker"> </bean> </beans>
一旦你在源文件和 bean 配置文件中完成了上面兩處改變,讓我們運行一下應用程序。如果你的應用程序一切都正常的話,這將會輸出以下消息:
Inside TextEditor constructor.
Inside SpellChecker constructor.
Inside checkSpelling.
默認情況下,@Autowired 注釋意味著依賴是必須的,它類似于 @Required 注釋,然而,你可以使用 @Autowired 的 (required=false) 選項關閉默認行為。
即使你不為 age 屬性傳遞任何參數,下面的示例也會成功運行,但是對于 name 屬性則需要一個參數。你可以自己嘗試一下這個示例,因為除了只有 Student.java 文件被修改以外,它和 @Required 注釋示例是相似的。
package com.tutorialspoint;
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;
}
}