無(wú)論選擇是什么,Spring 都能容納這兩種風(fēng)格,甚至可以將它們混合在一起。值得指出的是,通過(guò)它的 JAVA 配置選項(xiàng),Spring 允許注解以一種非入侵的方式使用,不觸碰目標(biāo)組件源碼和那些工具,所有的配置風(fēng)格由 Spring 工具套件支持。
基于注解的配置提供了一種 XML 設(shè)置的可替代方式,它依賴于字節(jié)碼元數(shù)據(jù)來(lái)連接組件,而不是用尖括號(hào)聲明的方式。代替使用 XML 來(lái)描述 bean 連接,開發(fā)者通過(guò)將注解使用在相關(guān)的類,方法或字段聲明中,將配置移動(dòng)到了組件類本身的內(nèi)部。正如在“Example: The
RequiredAnnotationBeanPostProcessor”那節(jié)提到的那樣,使用BeanPostProcessor與注解結(jié)合是擴(kuò)展 Spring IoC 容器的的常見方法。例如,Spring 2.0 引入了@Required注解來(lái)執(zhí)行需要的屬性的可能性。Spring 2.5 使以同樣地通用方法來(lái)驅(qū)動(dòng) Spring 的依賴注入變?yōu)榭赡堋1举|(zhì)上來(lái)說(shuō),@Autowired提供了如 3.4.5 小節(jié)描述的同樣的能力。“Autowiring collaborators”但更細(xì)粒度的控制和更廣的應(yīng)用性。Spring 2.5 也添加對(duì) JSR-250 注解的支持,例如,@PostConstruct和@PreDestroy
。Spring 3.0 添加了對(duì) JSR-330,包含在javax.inject包內(nèi)的注解(Java 的依賴注入)的支持,例如@Inject和@Named。關(guān)于這些注解的細(xì)節(jié)可以在相關(guān)的小節(jié)找到。
注解注入在 XML 注入之前進(jìn)行,因此對(duì)于通過(guò)兩種方法進(jìn)行組裝的屬性后者的配置會(huì)覆蓋前者。
跟以前一樣,你可以作為單獨(dú)的 bean 定義來(lái)注冊(cè)它們,但也可以通過(guò)在一個(gè)基于 XML 的 Spring 配置(注入包含上下文命名空間)中包含下面的標(biāo)簽來(lái)隱式的注冊(cè)它們:
<?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/
</beans>
(隱式注冊(cè)的后處理器包括
AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessor,
PersistenceAnnotationBeanPostProcessor和前面提到的
RequiredAnnotationBeanPostProcessor。)
<
context:annotation-config/>僅在定義它的同樣的應(yīng)用上下文中尋找注解的 beans。這意味著,如果你在一個(gè)為DispatcherServlet服務(wù)的WebApplicationContext中放置了<context:annotation-config/>,它只能在你的控制器中尋找@Autowired注解的 beans,而不是在你的服務(wù)層中。更多信息請(qǐng)看 18.2 小節(jié),“The DispatcherServlet”。
3.9.1 @Required
@Required注解應(yīng)用到 bean 屬性的 setter 方法上,例子如下:
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Required
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
這個(gè)注解僅僅是表明受影響的 bean 屬性必須在配置時(shí)通過(guò)顯式的 bean 定義或自動(dòng)組裝填充。如果受影響的 bean 屬性沒(méi)有填充,容器會(huì)拋出一個(gè)異常,這允許及早明確的失敗,避免NullPointerExceptions或后面出現(xiàn)類似的情況。仍然建議你在 bean 類本身加入斷言,例如,加入到初始化方法中。這樣做可以強(qiáng)制這些需要的引用和值,甚至是你在容器外部使用這個(gè)類的時(shí)候。
2 @Autowired
===========================================================================
在下面的例子中 JSR 330 的@Inject注解可以用來(lái)代替 Spring 的@Autowired注解。
你可以將@Autowired注解應(yīng)用到構(gòu)造函數(shù)上。
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
從 Spring 框架 4.3 起,如果目標(biāo) bena 僅定義了一個(gè)構(gòu)造函數(shù),那么@Autowired注解的構(gòu)造函數(shù)不再是必要的。如果一些構(gòu)造函數(shù)是可獲得的,至少有一個(gè)必須要加上注解,以便于告訴容器使用哪一個(gè)。
正如預(yù)料的那樣,你也可以將@Autowired注解應(yīng)用到“傳統(tǒng)的”setter 方法上:
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
你也可以應(yīng)用注解到具有任何名字和/或多個(gè)參數(shù)的方法上:
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
你也可以應(yīng)用@Autowired到字段上,甚至可以與構(gòu)造函數(shù)混合用:
public class MovieRecommender {
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
private MovieCatalog movieCatalog;
@Autowired
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
通過(guò)給帶有數(shù)組的字段或方法添加@Autowired注解,也可以從ApplicationContext中提供一組特定類型的 bean:
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}
同樣也可以應(yīng)用到具有同一類型的集合上:
public class MovieRecommender {
private Set<MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
如果你希望數(shù)組或列表中的項(xiàng)按指定順序排序,你的 bean 可以實(shí)現(xiàn)
org.springframework.core.Ordered接口,或使用@Order或標(biāo)準(zhǔn)@Priority注解。
只要期望的 key 是String,那么類型化的 Maps 就可以自動(dòng)組裝。Map 的值將包含所有期望類型的 beans,key 將包含對(duì)應(yīng)的 bean 名字:
public class MovieRecommender {
private Map<String, MovieCatalog> movieCatalogs;
@Autowired
public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
this.movieCatalogs = movieCatalogs;
}
// ...
}
默認(rèn)情況下,當(dāng)沒(méi)有候選 beans 可獲得時(shí),自動(dòng)組裝會(huì)失敗;默認(rèn)的行為是將注解的方法,構(gòu)造函數(shù)和字段看作指明了需要的依賴。這個(gè)行為也可以通過(guò)下面的方式去改變。
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired(required=false)
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
每個(gè)類只有一個(gè)構(gòu)造函數(shù)可以標(biāo)記為必需的,但可以注解多個(gè)非必需的構(gòu)造函數(shù)。在這種情況下,會(huì)考慮這些候選者中的每一個(gè),Spring 使用最貪婪的構(gòu)造函數(shù),即依賴最滿足的構(gòu)造函數(shù),具有最大數(shù)目的參數(shù)。
建議在@Required注解之上使用@Autowired的required特性。required特性表明這個(gè)屬性自動(dòng)裝配是不需要的,如果這個(gè)屬性不能被自動(dòng)裝配,它會(huì)被忽略。另一方面@Required是更強(qiáng)大的,在它強(qiáng)制這個(gè)屬性被任何容器支持的 bean 設(shè)置。如果沒(méi)有值注入,會(huì)拋出對(duì)應(yīng)的異常。
你也可以對(duì)那些已知的具有可解析依賴的接口使用@Autowired:BeanFactory,ApplicationContext,Environment, ResourceLoader,ApplicationEventPublisher和MessageSource。這些接口和它們的擴(kuò)展接口,例如
ConfigurableApplicationContext或ResourcePatternResolver,可以自動(dòng)解析,不需要特別的設(shè)置。
public class MovieRecommender {
@Autowired
private ApplicationContext context;
public MovieRecommender() {
}
// ...
}
@Autowired、@Inject、@Resource和@Value都是通過(guò) Spring 的BeanPostProcessor實(shí)現(xiàn)處理的,這反過(guò)來(lái)意味著,你不能在自己的BeanPostProcessor或BeanFactoryPostProcessor中使用這些注解。
這些類型必須顯式通過(guò) XML 或使用 Spring 的@Bean方法來(lái)裝配。
3 用 @Primary 微調(diào)基于注解的自動(dòng)裝配
因?yàn)楦鶕?jù)類型的自動(dòng)裝配可能會(huì)導(dǎo)致多個(gè)候選目標(biāo),所以在選擇過(guò)程中進(jìn)行更多的控制經(jīng)常是有必要的。一種方式通過(guò) Spring 的@Primary注解來(lái)完成。當(dāng)有個(gè)多個(gè)候選 bean 要組裝到一個(gè)單值的依賴時(shí),@Primary表明指定的 bean 應(yīng)該具有更高的優(yōu)先級(jí)。如果確定一個(gè)’primary’ bean 位于候選目標(biāo)中間,它將是那個(gè)自動(dòng)裝配的值。
假設(shè)我們具有如下配置,將firstMovieCatalog定義為主要的MovieCatalog。
@Configuration
public class MovieConfiguration {
@Bean
@Primary
public MovieCatalog firstMovieCatalog() { ... }
@Bean
public MovieCatalog secondMovieCatalog() { ... }
// ...
}
根據(jù)這樣的配置,下面的MovieRecommender將用firstMovieCatalog進(jìn)行自動(dòng)裝配。
public class MovieRecommender {
@Autowired
private MovieCatalog movieCatalog;
// ...
}
對(duì)應(yīng)的 bean 定義如下:
<?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 class="example.SimpleMovieCatalog" primary="true">
</bean>
<bean class="example.SimpleMovieCatalog">
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
3.9.4 微調(diào)基于注解且?guī)в邢薅ǚ淖詣?dòng)裝配
當(dāng)有多個(gè)實(shí)例需要確定一個(gè)主要的候選對(duì)象時(shí),@Primary是一種按類型自動(dòng)裝配的有效方式。當(dāng)需要在選擇過(guò)程中進(jìn)行更多的控制時(shí),可以使用 Spring 的@Qualifier注解。為了給每個(gè)選擇一個(gè)特定的 bean,你可以將限定符的值與特定的參數(shù)聯(lián)系在一起,減少類型匹配集合。在最簡(jiǎn)單的情況下,這是一個(gè)純描述性值:
public class MovieRecommender {
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;
// ...
}
@Qualifier注解也可以指定單個(gè)構(gòu)造函數(shù)參數(shù)或方法參數(shù):
public class MovieRecommender {
private MovieCatalog movieCatalog;
private CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
CustomerPreferenceDao customerPreferenceDao) {
this.movieCatalog = movieCatalog;
this.customerPreferenceDao = customerPreferenceDao;
}
// ...
}
對(duì)應(yīng)的 bean 定義如下。限定符值為”main”的 bean 被組裝到有相同值的構(gòu)造函數(shù)參數(shù)中。
<?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 class="example.SimpleMovieCatalog">
<qualifier value="main"/>
</bean
<bean class="example.SimpleMovieCatalog">
<qualifier value="action"/>
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
對(duì)于回退匹配,bean 名字被認(rèn)為是默認(rèn)的限定符值。因此你可以定義一個(gè) id 為main的 bean 來(lái)代替內(nèi)嵌的限定符元素,會(huì)有同樣的匹配結(jié)果。然而,盡管你可以使用這個(gè)約定根據(jù)名字引用特定的 beans,但是@Autowired從根本上來(lái)講是使用可選的語(yǔ)義限定符來(lái)進(jìn)行類型驅(qū)動(dòng)注入的。這意味著限定符的值,即使回退到 bean 名稱,總是縮小語(yǔ)義類型匹配的集合;它們沒(méi)有從語(yǔ)義上將一個(gè)引用表達(dá)為一個(gè)唯一的 bean id。好的限定符值是”main”或”EMEA”或”persistent”,表達(dá)一個(gè)特定組件的性質(zhì),這個(gè)組件是獨(dú)立于 bean id的,即使前面例子中像這個(gè) bean 一樣的匿名 bean 會(huì)自動(dòng)生成 id。
正如前面討論的那樣,限定符也可以應(yīng)用到類型結(jié)合上,例如,Set<MovieCatalog>。在這個(gè)例子中,根據(jù)聲明的限定符匹配的所有 beans 作為一個(gè)集合進(jìn)行注入。這意味著限定符不必是唯一的;它們只是構(gòu)成過(guò)濾標(biāo)準(zhǔn)。例如,你可以定義多個(gè)具有同樣限定符值”action”的MovieCatalog,所有的這些都將注入到帶有注解@Qualifier("action")的Set<MovieCatalog>中。
如果你想通過(guò)名字表達(dá)注解驅(qū)動(dòng)的注入,不要主要使用@Autowired,雖然在技術(shù)上能通過(guò)@Qualifier值引用一個(gè) bean 名字。作為可替代產(chǎn)品,可以使用 JSR-250 @Resource注解,它在語(yǔ)義上被定義為通過(guò)組件唯一的名字來(lái)識(shí)別特定的目標(biāo)組件,聲明的類型與匹配過(guò)程無(wú)關(guān)。@Autowired有不同的語(yǔ)義:通過(guò)類型選擇候選 beans,特定的String限定符值被認(rèn)為只在類型選擇的候選目標(biāo)中,例如,在那些標(biāo)記為具有相同限定符標(biāo)簽的 beans 中匹配一個(gè)”account”限定符。
對(duì)于那些本身定義在集合/映射或數(shù)組類型中的 beans 來(lái)說(shuō),@Resource是一個(gè)很好的解決方案,適用于特定的集合或通過(guò)唯一名字區(qū)分的數(shù)組 bean。也就是說(shuō),自 Spring 4.3 起,集合/映射和數(shù)組類型中也可以通過(guò) Spring 的@Autowired類型匹配算法進(jìn)行匹配,只要元素類型信息在@Bean中保留,返回類型簽名或集合繼承體系。在這種情況下,限定符值可以用來(lái)在相同類型的集合中選擇,正如在前一段中概括的那樣。
自 Spring 4.3 起,@Autowired也考慮自引用注入,例如,引用返回當(dāng)前注入的 bean。注意自注入是備用;普通對(duì)其它組件的依賴關(guān)系總是優(yōu)先的。在這個(gè)意義上,自引用不參與普通的候選目標(biāo)選擇,因此尤其是從不是主要的;恰恰相反,它們最終總是最低的優(yōu)先級(jí)。在實(shí)踐中,自引用只是作為最后的手段,例如,通過(guò) bean 的事務(wù)代理調(diào)用同一實(shí)例的其它方法:在考慮抽出受影響的方法來(lái)分隔代理 bean 的場(chǎng)景中。或者,使用@Resource通過(guò)它的唯一名字可能得到一個(gè)返回當(dāng)前 bean 的代理。
@Autowired可以應(yīng)用到字段,構(gòu)造函數(shù)和多參數(shù)方法上,允許通過(guò)限定符注解在參數(shù)層面上縮減候選目標(biāo)。相比之下,@Resource僅支持字段和 bean 屬性的帶有單個(gè)參數(shù)的 setter 方法。因此,如果你的注入目標(biāo)是一個(gè)構(gòu)造函數(shù)或一個(gè)多參數(shù)的方法,堅(jiān)持使用限定符。
你可以創(chuàng)建自己的定制限定符注解。簡(jiǎn)單定義一個(gè)注解,在你自己的定義中提供@Qualifier注解:
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {
String value();
}
然后你可以在自動(dòng)裝配的字段和參數(shù)上提供定制的限定符:
public class MovieRecommender {
@Autowired
@Genre("Action")
private MovieCatalog actionCatalog;
private MovieCatalog comedyCatalog;
@Autowired
public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
this.comedyCatalog = comedyCatalog;
}
// ...
}
接下來(lái),提供候選 bean 定義的信息。你可以添加<qualifier/>標(biāo)記作為<bean/>標(biāo)記的子元素,然后指定匹配你的定制限定符注解的類型和值。類型用來(lái)匹配注解的全限定類名稱。或者,如果沒(méi)有名稱沖突的風(fēng)險(xiǎn),為了方便,你可以使用簡(jiǎn)寫的類名稱。下面的例子證實(shí)了這些方法。
<?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 class="example.SimpleMovieCatalog">
<qualifier type="Genre" value="Action"/>
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier type="example.Genre" value="Comedy"/>
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
在 3.10 小節(jié),“類路徑掃描和管理組件”中,你將看到一個(gè)基于注解的替代方法,在 XML 中提供限定符元數(shù)據(jù)。特別地,看 3.10.8 小節(jié),“用注解提供限定符元數(shù)據(jù)”。
在某些情況下,使用沒(méi)有值的注解就是足夠的。當(dāng)注解為了通用的目的時(shí),這是非常有用的,可以應(yīng)用到跨幾個(gè)不同類型的依賴上。例如,當(dāng)網(wǎng)絡(luò)不可用時(shí),你可以提供一個(gè)要搜索的離線目錄。首先定義一個(gè)簡(jiǎn)單的注解:
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Offline {
}
然后將注解添加到要自動(dòng)裝配的字段或?qū)傩陨希?/p>
public class MovieRecommender {
@Autowired
@Offline
private MovieCatalog offlineCatalog;
// ...
}
現(xiàn)在 bean 定義只需要一個(gè)限定符類型:
<bean class="example.SimpleMovieCatalog">
<qualifier type="Offline"/>
</bean>
你也可以定義接收命名屬性之外的定制限定符注解或代替簡(jiǎn)單的值屬性。如果要注入的字段或參數(shù)指定了多個(gè)屬性值,bean 定義必須匹配所有的屬性值才會(huì)被認(rèn)為是一個(gè)可自動(dòng)裝配的候選目標(biāo)。作為一個(gè)例子,考慮下面的注解定義:
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface MovieQualifier {
String genre();
Format format();
}
這種情況下Format是枚舉類型:
public enum Format {
VHS, DVD, BLURAY
}
要自動(dòng)裝配的字段使用定制限定符進(jìn)行注解,并且包含了兩個(gè)屬性值:genre和format。
public class MovieRecommender {
@Autowired






