Spring Java-based容器配置詳解
裝Java-based的配置
使用 @Import 注解
跟在Spring XML文件中使用<import>元素添加模塊化的配置類(lèi)似,@Import注解允許你加載其他配置類(lèi)中的@Bean定義:
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}
組現(xiàn)在,當(dāng)實(shí)例化上下文時(shí),你只需要顯式的指定ConfigB,而不需要既提供ConfigA.class,又提供ConfigB.class:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class);
// now both beans A and B will be available...
A a = ctx.getBean(A.class);
B b = ctx.getBean(B.class);
}
這種方式簡(jiǎn)化了容器的初始化,因?yàn)橹恍枰幚硪粋€(gè)類(lèi),而不是讓開(kāi)發(fā)者記住構(gòu)造期間的大量@Configuration類(lèi)。
導(dǎo)入@Bean的依賴(lài)注入
上面的示例可以工作,但太簡(jiǎn)單。在大多數(shù)實(shí)際的場(chǎng)景中,beans會(huì)依賴(lài)另一個(gè)跨配置類(lèi)的bean。當(dāng)使用XML時(shí),這不是問(wèn)題,因?yàn)椴簧婕暗骄幾g,其中一個(gè)bean只需要聲明ref="someBean",剩下的交給Spring在容器初始化期間處理即可。當(dāng)然,當(dāng)使用@Configuration類(lèi)時(shí),Java編譯器對(duì)配置模式產(chǎn)生一些限制,對(duì)其他beans的引用必須是合法的java語(yǔ)法。
幸運(yùn)的是,解決該問(wèn)題是很容易的。正如我們已經(jīng)討論的,@Bean可以有任意多個(gè)用來(lái)描述bean依賴(lài)的參數(shù)。讓我們探討一個(gè)更現(xiàn)實(shí)的場(chǎng)景,在這里將使用一些彼此依賴(lài)的@Configuration類(lèi):
@Configuration
public class ServiceConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}
@Configuration
public class RepositoryConfig {
@Bean
public AccountRepository accountRepository(DataSource dataSource) {
return new JdbcAccountRepository(dataSource);
}
}
@Configuration
@Import({
ServiceConfig.class, RepositoryConfig.class
}
)
public class SystemTestConfig {
@Bean
public DataSource dataSource() {
// return new DataSource
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
// everything wires up across configuration classes...
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}
這里有另外的方法可以達(dá)到相同的效果。記住,@Configuration根本上只是容器中的另一個(gè)bean-這意味著它們可以像其他bean那樣充分利用@Autowired注入元數(shù)據(jù)。
注: 確保以這種方式注入的都是簡(jiǎn)單類(lèi)型的。@Configuration類(lèi)在容器初始化時(shí)被處理的相當(dāng)早,用這種方式強(qiáng)制注入依賴(lài)可能導(dǎo)致無(wú)法預(yù)料地過(guò)早初始化問(wèn)題。只要有可能就采用上面示例中基于參數(shù)的注入方式。
@Configuration
public class ServiceConfig {
@Autowired
private AccountRepository accountRepository;
@Bean
public TransferService transferService() {
return new TransferServiceImpl(accountRepository);
}
}
@Configuration
public class RepositoryConfig {
@Autowired
private DataSource dataSource;
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(dataSource);
}
}
@Configuration
@Import({
ServiceConfig.class, RepositoryConfig.class
}
)
public class SystemTestConfig {
@Bean
public DataSource dataSource() {
// return new DataSource
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
// everything wires up across configuration classes...
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}
在上面的示例中,使用@Autowired工作的很好,并且提供了想要的模塊化,但要確切地指明自動(dòng)注入的bean定義在哪聲明依舊有點(diǎn)模糊。例如,一個(gè)開(kāi)發(fā)者正在查看ServiceConfig,那你怎么準(zhǔn)確地知道@Autowired AccountRepository bean在哪聲明的?在代碼中并不明確,不過(guò)有時(shí)候這樣就行。記著Spring Tool Suite可以提供渲染圖的工具,這些圖展示了Spring Bean之間是怎么連起來(lái)的-這可能是你需要的。同時(shí),你的Java IDE可以輕松的找到所有聲明和使用AccountRepository類(lèi)型的bean,并為你快速展現(xiàn)返回該類(lèi)型的@Bean方法位置。
如果你不能接受這種模糊性,并希望在你的IDE中可以從一個(gè)@Configuration類(lèi)導(dǎo)航到另一個(gè),那就考慮注入配置類(lèi)本身:
@Configuration
public class ServiceConfig {
@Autowired
private RepositoryConfig repositoryConfig;
@Bean
public TransferService transferService() {
// navigate 'through' the config class to the @Bean method!
return new TransferServiceImpl(repositoryConfig.accountRepository());
}
}
在上面的解決方案中,我們可以很明確地知道AccountRepository定義的地方。然而,ServiceConfig現(xiàn)在緊緊地跟RepositoryConfig耦合了。這就是權(quán)衡。緊耦合在某種程度上可以通過(guò)使用基于接口或抽象類(lèi)的@Configuration類(lèi)來(lái)減輕??紤]以下內(nèi)容:
@Configuration
public class ServiceConfig {
@Autowired
private RepositoryConfig repositoryConfig;
@Bean
public TransferService transferService() {
return new TransferServiceImpl(repositoryConfig.accountRepository());
}
}
@Configuration
public interface RepositoryConfig {
@Bean
AccountRepository accountRepository();
}
@Configuration
public class DefaultRepositoryConfig implements RepositoryConfig {
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(...);
}
}
@Configuration
@Import({
ServiceConfig.class, DefaultRepositoryConfig.class
}
) // import the concrete config!
public class SystemTestConfig {
@Bean
public DataSource dataSource() {
// return DataSource
}
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(SystemTestConfig.class);
TransferService transferService = ctx.getBean(TransferService.class);
transferService.transfer(100.00, "A123", "C456");
}
現(xiàn)在,ServiceConfig跟具體的DefaultRepositoryConfig類(lèi)是松耦合的關(guān)系,并且內(nèi)嵌的IDE工具依舊有用:它很容易為開(kāi)發(fā)者獲取RepositoryConfig實(shí)現(xiàn)的類(lèi)型層次。采用這種方式,導(dǎo)航@Configuration和它們的依賴(lài)就變得跟平常處理基于接口的代碼導(dǎo)航?jīng)]區(qū)別了。
有條件的包含@Configuration類(lèi)或@Beans
基于任意的系統(tǒng)狀態(tài),有條件地禁用一個(gè)完整的@Configuration類(lèi),甚至單獨(dú)的@Bean方法通常是很有用的。一個(gè)常見(jiàn)的示例是,當(dāng)一個(gè)特定的profile在Spring Environment中啟用時(shí),使用@Profile注解激活beans。
@Profile注解實(shí)際上實(shí)現(xiàn)了一個(gè)非常靈活的注解:@Conditional。@Conditional注解意味著在注冊(cè)@Bean之前,必須先咨詢(xún)指定的org.springframework.context.annotation.Condition實(shí)現(xiàn)。
Condition接口的實(shí)現(xiàn)者只需簡(jiǎn)單地提供一個(gè)返回true或false的matches(…)方法。例如,下面是@Profile注解采用的Condition實(shí)現(xiàn):
@Override
public Boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
if (context.getEnvironment() != null) {
// Read the @Profile annotation attributes
MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
if (attrs != null) {
for (Object value : attrs.get("value")) {
if (context.getEnvironment().acceptsProfiles(((String[]) value))) {
return true;
}
}
return false;
}
}
return true;
}
結(jié)合Java和XML配置
Spring @Configuration類(lèi)支持目的不是想要100%的替換Spring XML。一些設(shè)施,比如Spring XML命名空間仍舊是配置容器的完美方式。在XML很方便或必須的情況下,你有個(gè)選擇:采用”XML為中心”的方式實(shí)例化容器,比如ClassPathXmlApplicationContext,或使用AnnotationConfigApplicationContext以”Java為中心”的方式,并使用@ImportResource注解導(dǎo)入需要的XML。
在以”XML為中心”的情況下使用@Configuration類(lèi)
從XML啟動(dòng)Spring容器,以特設(shè)模式包含@Configuration類(lèi)可能是個(gè)更可選的方式。例如,在一個(gè)已經(jīng)存在的使用Spring XML的大型代碼庫(kù)中,遵循按需原則創(chuàng)建@Configuration,并從現(xiàn)有的XML文件中包括它們是非常容易的。下面你將找到在這樣的”XML為中心”的解決方案中使用@Configuration類(lèi)的可選項(xiàng)。
記著@Configuration類(lèi)本質(zhì)上只是容器中的bean定義。在下面的示例中,我們創(chuàng)建了一個(gè)名稱(chēng)為AppConfig的@Configuration類(lèi),并將它作為<bean/>定義包含到system-test-config.xml中。因?yàn)?lt;context:annotation-config/>是開(kāi)啟的,容器將會(huì)識(shí)別@Configuration注解,并正確地處理AppConfig中聲明的@Bean方法。
@Configuration
public class AppConfig {
@Autowired
private DataSource dataSource;
@Bean
public AccountRepository accountRepository() {
return new JdbcAccountRepository(dataSource);
}
@Bean
public TransferService transferService() {
return new TransferService(accountRepository());
}
}
ystem-test-config.xml如下:
<beans>
<!-- enable processing of annotations such as @Autowired and @Configuration -->
<context:annotation-config/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="com.acme.AppConfig"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
jdbc.properties如下:
jdbc.properties jdbc.url=jdbc:hsqldb:hsql://localhost/xdb jdbc.username=sa jdbc.password=
main方法如下:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/acme/system-test-config.xml");
TransferService transferService = ctx.getBean(TransferService.class);
// ...
}
注: 在上面的system-test-config.xml中,AppConfig<bean/>沒(méi)有聲明一個(gè)id元素。如果沒(méi)有bean引用它,那就沒(méi)有必要指定id元素,否則就要通過(guò)name從容器獲取bean(name對(duì)應(yīng)bean定義中聲明的id)。DataSource也一樣-它只是通過(guò)類(lèi)型自動(dòng)注入(autowired by type),所以并不需要顯式的分配一個(gè)bean id。
由于@Configuration被@Component元注解了(被注解注解,很拗口),所以被@Configuration注解的類(lèi)自動(dòng)成為組件掃描(component scanning)的候選者。同樣使用上面的場(chǎng)景,我們可以重新定義system-test-config.xml來(lái)充分利用組件掃描。注意在這個(gè)示例中,我們不需要明確聲明<context:annotation-config/>,因?yàn)?lt;context:component-scan/>啟用了同樣的功能。
system-test-config.xml如下:
<beans>
<!-- picks up and registers AppConfig as a bean definition -->
<context:component-scan base-package="com.acme"/>
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
在@Configuration”類(lèi)為中心”的情況下使用@ImportResourcedaoru導(dǎo)入XML
在將@Configuration類(lèi)作為配置容器的主要機(jī)制的應(yīng)用中,仍舊存在對(duì)XML的需求。在那些場(chǎng)景中,可以使用@ImportResource,并定義所需的XML。這樣做可以實(shí)現(xiàn)以”Java為中心”的方式配置容器,并保留最低限度的XML。
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource() {
return new DriverManagerDataSource(url, username, password);
}
}
properties-config.xml如下:
<beans> <context:property-placeholder location="classpath:/com/acme/jdbc.properties"/> </beans>
jdbc.properties如下:
jdbc.url=jdbc:hsqldb:hsql://localhost/xdb jdbc.username=sa jdbc.password=
main方法如下:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
TransferService transferService = ctx.getBean(TransferService.class);
// ...
}
總結(jié)
以上就是本文關(guān)于Spring Java-based容器配置詳解的全部?jī)?nèi)容,感興趣的朋友可以繼續(xù)參閱:Java之Spring注解配置bean實(shí)例代碼解析、淺談Java注解和動(dòng)態(tài)代理等,希望對(duì)大家有所幫助。有不足之處,歡迎留言指正。感謝朋友們對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解如何使用Java8?Steam流對(duì)Map進(jìn)行排序
這篇文章主要給大家詳細(xì)介紹了如何使用Java8?Steam流對(duì)Map進(jìn)行排序,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01
Sentinel網(wǎng)關(guān)限流與SpringCloud Gateway整合過(guò)程
本文介紹了如何通過(guò)SpringCloudGateway集成阿里的Sentinel進(jìn)行網(wǎng)關(guān)限流,Sentinel作為流量防衛(wèi)兵,提供了豐富的應(yīng)用場(chǎng)景和完備的實(shí)時(shí)監(jiān)控功能,通過(guò)配置路由維度和自定義API維度的限流規(guī)則,實(shí)現(xiàn)了對(duì)微服務(wù)的保護(hù)2024-11-11
Springboot事件和bean生命周期執(zhí)行機(jī)制實(shí)例詳解
這篇文章主要介紹了Springboot事件和bean的生命周期執(zhí)行機(jī)制,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
關(guān)于SpringBoot微服務(wù)發(fā)布與部署的三種方式
SpringBoot 框架只提供了一套基于可執(zhí)行 jar 包(executable jar)格式的標(biāo)準(zhǔn)發(fā)布形式,但并沒(méi)有對(duì)部署做過(guò)多的界定,而且為了簡(jiǎn)化可執(zhí)行 jar 包的生成,SpringBoot 提供了相應(yīng)的 Maven 項(xiàng)目插件,需要的朋友可以參考下2023-05-05
java實(shí)現(xiàn)簡(jiǎn)單注冊(cè)選擇所在城市
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單注冊(cè)選擇所在城市的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04
詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑
這篇文章主要介紹了詳解Spring框架下向異步線程傳遞HttpServletRequest參數(shù)的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03

