文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書(shū)
3.9 基于注解的容器配置
在配置Spring時(shí)注解是否比XML更好?
基于注解配置的引入引出了一個(gè)問(wèn)題——這種方式是否比基于XML的配置更好。簡(jiǎn)短的回答是視情況而定。長(zhǎng)一點(diǎn)的回答是每種方法都有它的優(yōu)點(diǎn)和缺點(diǎn),通常是由開(kāi)發(fā)者決定哪一種策略更適合他們。由于注解的定義方式,注解在它們的聲明中提供了許多上下文,導(dǎo)致配置更簡(jiǎn)短更簡(jiǎn)潔。然而,XML擅長(zhǎng)連接組件而不必接觸源代碼或重新編譯它們。一些開(kāi)發(fā)者更喜歡接近源代碼,而另一些人則認(rèn)為基于注解的類(lèi)不再是POJOs,此外,配置變的去中心化,而且更難控制。
無(wú)論選擇是什么,Spring都能容納這兩種風(fēng)格,甚至可以將它們混合在一起。值得指出的是,通過(guò)它的Java配置選項(xiàng),Spring允許注解以一種非入侵的方式使用,不觸碰目標(biāo)組件源碼和那些工具,所有的配置風(fēng)格由Spring工具套件支持。
基于注解的配置提供了一種XML設(shè)置的可替代方式,它依賴(lài)于字節(jié)碼元數(shù)據(jù)來(lái)連接組件,而不是用尖括號(hào)聲明的方式。代替使用XML來(lái)描述bean連接,開(kāi)發(fā)者通過(guò)將注解使用在相關(guān)的類(lèi),方法或字段聲明中,將配置移動(dòng)到了組件類(lèi)本身的內(nèi)部。正如在“Example: The RequiredAnnotationBeanPostProcessor”那節(jié)提到的那樣,使用BeanPostProcessor與注解結(jié)合是擴(kuò)展Spring IoC容器的的常見(jiàn)方法。例如,Spring 2.0引入了@Required注解來(lái)執(zhí)行需要的屬性的可能性。Spring 2.5使以同樣地通用方法來(lái)驅(qū)動(dòng)Spring的依賴(lài)注入變?yōu)榭赡?。本質(zhì)上來(lái)說(shuō),@Autowired提供了如3.4.5小節(jié)描述的同樣的能力?!癆utowiring collaborators”但更細(xì)粒度的控制和更廣的應(yīng)用性。Spring 2.5也添加對(duì)JSR-250注解的支持,例如,@PostConstruct和@PreDestroy
。Spring 3.0添加了對(duì)JSR-330,包含在javax.inject包內(nèi)的注解(Java的依賴(lài)注入)的支持,例如@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)類(lèi)似的情況。仍然建議你在bean類(lèi)本身加入斷言,例如,加入到初始化方法中。這樣做可以強(qiáng)制這些需要的引用和值,甚至是你在容器外部使用這個(gè)類(lèi)的時(shí)候。
3.9.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中提供一組特定類(lèi)型的bean:
public class MovieRecommender {
@Autowired
private MovieCatalog[] movieCatalogs;
// ...
}
同樣也可以應(yīng)用到具有同一類(lèi)型的集合上:
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,那么類(lèi)型化的Maps就可以自動(dòng)組裝。Map的值將包含所有期望類(lèi)型的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ù)和字段看作指明了需要的依賴(lài)。這個(gè)行為也可以通過(guò)下面的方式去改變。
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired(required=false)
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
每個(gè)類(lèi)只有一個(gè)構(gòu)造函數(shù)可以標(biāo)記為必需的,但可以注解多個(gè)非必需的構(gòu)造函數(shù)。在這種情況下,會(huì)考慮這些候選者中的每一個(gè),Spring使用最貪婪的構(gòu)造函數(shù),即依賴(lài)最滿(mǎn)足的構(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ì)那些已知的具有可解析依賴(lài)的接口使用@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ò)SpringBeanPostProcessor實(shí)現(xiàn)處理,這反過(guò)來(lái)意味著你不能在你自己的BeanPostProcessor或BeanFactoryPostProcessor中應(yīng)用這些注解(如果有的話(huà))。這些類(lèi)型必須顯式的通過(guò)XML或使用Spring的@Bean方法來(lái)'wired up'。
3.9.3 用@Primary微調(diào)基于注解的自動(dòng)裝配
因?yàn)楦鶕?jù)類(lèi)型的自動(dòng)裝配可能會(huì)導(dǎo)致多個(gè)候選目標(biāo),所以在選擇過(guò)程中進(jìn)行更多的控制經(jīng)常是有必要的。一種方式通過(guò)Spring的@Primary注解來(lái)完成。當(dāng)有個(gè)多個(gè)候選bean要組裝到一個(gè)單值的依賴(lài)時(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">
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<!-- inject any dependencies required by this bean -->
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
3.9.4 微調(diào)基于注解且?guī)в邢薅ǚ淖詣?dòng)裝配
當(dāng)有多個(gè)實(shí)例需要確定一個(gè)主要的候選對(duì)象時(shí),@Primary是一種按類(lèi)型自動(dòng)裝配的有效方式。當(dāng)需要在選擇過(guò)程中進(jìn)行更多的控制時(shí),可以使用Spring的@Qualifier注解。為了給每個(gè)選擇一個(gè)特定的bean,你可以將限定符的值與特定的參數(shù)聯(lián)系在一起,減少類(lèi)型匹配集合。在最簡(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"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier value="action"/>
<!-- inject any dependencies required by this bean -->
</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)行類(lèi)型驅(qū)動(dòng)注入的。這意味著限定符的值,即使回退到bean名稱(chēng),總是縮小語(yǔ)義類(lèi)型匹配的集合;它們沒(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)用到類(lèi)型結(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)組件,聲明的類(lèi)型與匹配過(guò)程無(wú)關(guān)。@Autowired有不同的語(yǔ)義:通過(guò)類(lèi)型選擇候選beans,特定的String限定符值被認(rèn)為只在類(lèi)型選擇的候選目標(biāo)中,例如,在那些標(biāo)記為具有相同限定符標(biāo)簽的beans中匹配一個(gè)"account"限定符。
對(duì)于那些本身定義在集合/映射或數(shù)組類(lèi)型中的beans來(lái)說(shuō),
@Resource是一個(gè)很好的解決方案,適用于特定的集合或通過(guò)唯一名字區(qū)分的數(shù)組bean。也就是說(shuō),自Spring 4.3起,集合/映射和數(shù)組類(lèi)型中也可以通過(guò)Spring的@Autowired類(lèi)型匹配算法進(jìn)行匹配,只要元素類(lèi)型信息在@Bean中保留,返回類(lèi)型簽名或集合繼承體系。在這種情況下,限定符值可以用來(lái)在相同類(lèi)型的集合中選擇,正如在前一段中概括的那樣。
自Spring 4.3起,
@Autowired也考慮自引用注入,例如,引用返回當(dāng)前注入的bean。注意自注入是備用;普通對(duì)其它組件的依賴(lài)關(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)型和值。類(lèi)型用來(lái)匹配注解的全限定類(lèi)名稱(chēng)。或者,如果沒(méi)有名稱(chēng)沖突的風(fēng)險(xiǎn),為了方便,你可以使用簡(jiǎn)寫(xiě)的類(lèi)名稱(chēng)。下面的例子證實(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"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier type="example.Genre" value="Comedy"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean id="movieRecommender" class="example.MovieRecommender"/>
</beans>
在3.10小節(jié),“類(lèi)路徑掃描和管理組件”中,你將看到一個(gè)基于注解的替代方法,在XML中提供限定符元數(shù)據(jù)。特別地,看3.10.8小節(jié),“用注解提供限定符元數(shù)據(jù)”。
在某些情況下,使用沒(méi)有值的注解就是足夠的。當(dāng)注解為了通用的目的時(shí),這是非常有用的,可以應(yīng)用到跨幾個(gè)不同類(lèi)型的依賴(lài)上。例如,當(dāng)網(wǎng)絡(luò)不可用時(shí),你可以提供一個(gè)要搜索的離線(xiàn)目錄。首先定義一個(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è)限定符類(lèi)型:
<bean class="example.SimpleMovieCatalog">
<qualifier type="Offline"/>
<!-- inject any dependencies required by this bean -->
</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是枚舉類(lèi)型:
public enum Format {
VHS, DVD, BLURAY
}
要自動(dòng)裝配的字段使用定制限定符進(jìn)行注解,并且包含了兩個(gè)屬性值:genre和format。
public class MovieRecommender {
@Autowired
@MovieQualifier(format=Format.VHS, genre="Action")
private MovieCatalog actionVhsCatalog;
@Autowired
@MovieQualifier(format=Format.VHS, genre="Comedy")
private MovieCatalog comedyVhsCatalog;
@Autowired
@MovieQualifier(format=Format.DVD, genre="Action")
private MovieCatalog actionDvdCatalog;
@Autowired
@MovieQualifier(format=Format.BLURAY, genre="Comedy")
private MovieCatalog comedyBluRayCatalog;
// ...
}
最后,bean定義應(yīng)該包含匹配的限定符值。這個(gè)例子也證實(shí)了bean元屬性可以用來(lái)代替<qualifier/>子元素。如果可獲得<qualifier/>,它和它的屬性?xún)?yōu)先級(jí)更高,如果當(dāng)前沒(méi)有限定符,自動(dòng)裝配機(jī)制會(huì)將<meta/>內(nèi)的值作為備用,正如下面的例子中的最后兩個(gè)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">
<qualifier type="MovieQualifier">
<attribute key="format" value="VHS"/>
<attribute key="genre" value="Action"/>
</qualifier>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<qualifier type="MovieQualifier">
<attribute key="format" value="VHS"/>
<attribute key="genre" value="Comedy"/>
</qualifier>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<meta key="format" value="DVD"/>
<meta key="genre" value="Action"/>
<!-- inject any dependencies required by this bean -->
</bean>
<bean class="example.SimpleMovieCatalog">
<meta key="format" value="BLURAY"/>
<meta key="genre" value="Comedy"/>
<!-- inject any dependencies required by this bean -->
</bean>
</beans>
3.9.5 使用泛型作為自動(dòng)裝配限定符
除了@Qualifier注解外,也可以使用Java的泛型類(lèi)型作為限定符的一種暗示方式。例如,假設(shè)你有如下配置:
@Configuration
public class MyConfiguration {
@Bean
public StringStore stringStore() {
return new StringStore();
}
@Bean
public IntegerStore integerStore() {
return new IntegerStore();
}
}
假設(shè)上面的beans實(shí)現(xiàn)了一個(gè)泛型接口,例如,Store<String>和Store<Integer>,你可以@Autowire Store接口,泛型將作為限定符使用:
@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean
@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean
當(dāng)自動(dòng)裝配Lists,Maps和Arrays時(shí),也會(huì)應(yīng)用泛型限定符:
// Inject all Store beans as long as they have an <Integer> generic
// Store<String> beans will not appear in this list
@Autowired
private List<Store<Integer>> s;
3.9.6 CustomAutowireConfigurer
CustomAutowireConfigurer是一個(gè)能使你注冊(cè)自己的定制限定符注解類(lèi)型的BeanFactoryPostProcessor,即使它們不使用Spring的@Qualifier注解進(jìn)行注解。
<bean id="customAutowireConfigurer"
class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>example.CustomQualifier</value>
</set>
</property>
</bean>
AutowireCandidateResolver通過(guò)下面的方式?jīng)Q定自動(dòng)裝配的候選目標(biāo):
每個(gè)bean定義的
autowire-candidate在
<beans/>元素可獲得的任何default-autowire-candidates模式存在
@Qualifier注解和任何在CustomAutowireConfigurer中注冊(cè)的定制注解
當(dāng)多個(gè)beans符合條件成為自動(dòng)裝配的候選目標(biāo)時(shí),"primary" bean的決定如下:如果在候選目標(biāo)中某個(gè)確定的bean中的primary特性被設(shè)為true,它將被選為目標(biāo)bean。
3.9.7 @Resource
Spring也支持使用JSR-250 @Resource對(duì)字段或bean屬性setter方法進(jìn)行注入。這是在Java EE 5和6中的一種通用模式,例如在JSF 1.2管理的beans或JAX-WS 2.0的端點(diǎn)。Spring對(duì)它管理的對(duì)象也支持這種模式。
@Resource采用名字屬性,默認(rèn)情況下Spring將名字值作為要注入的bean的名字。換句話(huà)說(shuō),它遵循by-name語(yǔ)義,下面的例子證實(shí)了這一點(diǎn):
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource(name="myMovieFinder")
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
如果沒(méi)有顯式的指定名字,默認(rèn)名字從字段名或setter方法中取得。在字段情況下,它采用字段名稱(chēng);在setter方法情況下,它采用bean的屬性名。因此下面的例子將名字為movieFinder的bean注入到它的setter方法中:
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Resource
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
}
注解提供的名字被
CommonAnnotationBeanPostProcessor感知的ApplicationContext解析為bean名字。如果你顯式地配置了Spring的SimpleJndiBeanFactory,名字會(huì)通過(guò)JNDI解析。但是建議你依賴(lài)默認(rèn)行為,簡(jiǎn)單使用Spring的JNDI查找功能保護(hù)間接查找級(jí)別。
在@Resource特有的沒(méi)有顯式名字指定的情況下,類(lèi)似于@Autowired,@Resource會(huì)進(jìn)行主要的匹配類(lèi)型來(lái)代替指定名字的bean并解析已知的可解析依賴(lài):BeanFactory,ApplicationContext,ResourceLoader,ApplicationEventPublisher和MessageSource接口。
因此在下面的例子中,customerPreferenceDao字段首先查找名字為customerPreferenceDao的bean,然后回退到主要的類(lèi)型為CustomerPreferenceDao的類(lèi)型匹配。"context"字段會(huì)注入基于已知的可解析依賴(lài)類(lèi)型ApplicationContext。
public class MovieRecommender {
@Resource
private CustomerPreferenceDao customerPreferenceDao;
@Resource
private ApplicationContext context;
public MovieRecommender() {
}
// ...
}
3.9.8 @PostConstruct和@PreDestroy
CommonAnnotationBeanPostProcessor不僅識(shí)別@Resource注解,而且識(shí)別JSR-250生命周期注解。在Spring 2.5引入了對(duì)這些注解的支持,也提供了在初始化回調(diào)函數(shù)和銷(xiāo)毀回調(diào)函數(shù)中描述的那些注解的一種可替代方式。假設(shè)CommonAnnotationBeanPostProcessor在Spring的ApplicationContext中注冊(cè),執(zhí)行這些注解的方法在生命周期的同一點(diǎn)被調(diào)用,作為對(duì)應(yīng)的Spring生命周期接口方法或顯式聲明的回調(diào)方法。在下面的例子中,緩存會(huì)預(yù)先放置接近初始化之前,并在銷(xiāo)毀之前清除。
public class CachingMovieLister {
@PostConstruct
public void populateMovieCache() {
// populates the movie cache upon initialization...
}
@PreDestroy
public void clearMovieCache() {
// clears the movie cache upon destruction...
}
}
關(guān)于組合各種生命周期機(jī)制的影響的更多細(xì)節(jié),請(qǐng)看“組合生命周期機(jī)制”小節(jié)。