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