Spring 注解@Bean使用方式你都知道嗎?
環(huán)境:Spring5.3.25
@Bean是方法級注釋,是XML <bean/>元素的直接類比。注解支持<bean/>提供的一些屬性,例如:
- init-method
- destroy-method
- autowiring
- name.
你可以在@Configuration或@Component類中使用@Bean注釋。
聲明Bean
要聲明bean,可以使用@Bean注釋對方法進行注釋??梢允褂么朔椒ㄔ贏pplicationContext中注冊bean定義,其類型指定為該方法的返回值。默認(rèn)情況下,bean名與方法名相同。下面的例子展示了一個@Bean方法聲明:
@Configuration
public class AppConfig {
@Bean
public TransferServiceImpl transferService() {
return new TransferServiceImpl();
}
}
前面的配置與下面的Spring XML完全等效:
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
這兩個聲明都使一個名為transferService的bean在ApplicationContext中可用,綁定到類型為TransferServiceImpl的對象實例,如下圖所示:
transferService -> com.acme.TransferServiceImpl
你也可以使用默認(rèn)方法來定義bean。這允許通過在默認(rèn)方法上實現(xiàn)帶有bean定義的接口來組合bean配置。
public interface BaseConfig {
@Bean
default TransferServiceImpl transferService() {
return new TransferServiceImpl();
}
}
@Configuration
public class AppConfig implements BaseConfig {
}
你還可以用接口(或基類)返回類型聲明@Bean方法,如下例所示:
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
然而,這將高級類型預(yù)測的可見性限制為指定的接口類型(TransferService)。然后,只有在實例化了受影響的單例bean之后,容器才知道完整類型(TransferServiceImpl)。非惰性單例bean根據(jù)其聲明順序進行實例化,因此你可能會看到不同的類型匹配結(jié)果,這取決于另一個組件何時嘗試通過非聲明類型進行匹配(例如@Autowired TransferServiceImpl,它只在transferServicebean實例化后才解析)。
Bean依賴
一個@Bean注釋的方法可以有任意數(shù)量的參數(shù)來描述構(gòu)建該Bean所需的依賴關(guān)系。例如,如果我們的TransferService需要一個AccountRepository,我們可以用一個方法參數(shù)來實現(xiàn)該依賴關(guān)系,如下例所示:
@Configuration
public class AppConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}
解析機制與基于構(gòu)造函數(shù)的依賴注入非常相似。
Bean生命周期
使用@Bean注釋定義的任何類都支持常規(guī)的生命周期回調(diào),并且可以使用JSR-250中的@PostConstruct和@PreDestroy注釋。
也完全支持常規(guī)的Spring生命周期回調(diào)。如果bean實現(xiàn)InitializingBean、DisposableBean或Lifecycle,那么容器將調(diào)用它們各自的方法。
還完全支持一組標(biāo)準(zhǔn)的*Aware接口(如BeanFactoryAware、BeanNameAware、MessageSourceAware、ApplicationContextAware等)。
@Bean注釋支持指定任意的初始化和銷毀回調(diào)方法,就像Spring XML在Bean元素上的init方法和destroy方法屬性一樣,如下例所示:
public class BeanOne {
public void init() {
// initialization logic
}
}
public class BeanTwo {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public BeanOne beanOne() {
return new BeanOne();
}
@Bean(destroyMethod = "cleanup")
public BeanTwo beanTwo() {
return new BeanTwo();
}
}
默認(rèn)情況下,使用Java配置定義的具有公共close或shutdown方法的bean會自動使用銷毀回調(diào)進行登記。如果你有一個公共的close或shutdown方法,并且不希望在容器關(guān)閉時調(diào)用它,那么可以將@Bean(destroyMethod="")添加到Bean定義中,以禁用默認(rèn)(推斷)模式。
public class Main {
static class Person {
public void close() {
System.out.println("close") ;
}
public void shutdown() {
System.out.println("shutdown") ;
}
}
@Configuration
static class AppConfig {
@Bean
// @Bean(destroyMethod = "") 這樣就禁用了關(guān)閉的操作(close和shutdown)
public Person person() {
return new Person() ;
}
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class) ;
context.close() ;
}
}
注意:如果一個類中既有close方法又有shutdown方法,那么只有close方法生效。
以前面示例中的BeanOne為例,在構(gòu)造過程中直接調(diào)用init()方法同樣有效,如下面的例子所示:
@Configuration
public class AppConfig {
@Bean
public BeanOne beanOne() {
BeanOne beanOne = new BeanOne();
beanOne.init();
return beanOne;
}
}
當(dāng)你直接在Java中工作時,你可以對對象執(zhí)行任何你喜歡的操作,而不必總是依賴于容器生命周期。
Bean作用域
Spring包含@Scope注釋,以便您可以指定bean的范圍。
你可以指定使用@Bean注釋定義的Bean應(yīng)該具有特定的作用域??梢允褂肂ean scopes部分中指定的任何標(biāo)準(zhǔn)作用域。
默認(rèn)作用域是singleton,但您可以使用@Scope注釋來覆蓋它,如下例所示:
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}
@Scope and scoped-proxy
Spring通過作用域代理提供了一種方便的方式來處理作用域依賴。在使用XML配置時,創(chuàng)建這樣一個代理的最簡單方法是<aop:scope -proxy/>元素。在Java中配置bean時,使用@Scope注解可以為proxyMode屬性提供類似的支持。默認(rèn)值是ScopedProxyMode.DEFAULT,它通常表示不應(yīng)該創(chuàng)建scoped proxy,除非在組件掃描指令級別配置了不同的DEFAULT。你可以指定
ScopedProxyMode.TARGET_CLASS ScopedProxyMode。接口或ScopedProxyMode.NO。
如果你使用Java將XML參考文檔(參見“限定范圍的代理”)中的限定范圍代理示例移植到我們的@Bean,它類似于以下內(nèi)容:
// an HTTP Session-scoped bean exposed as a proxy
@Bean
@SessionScope
public UserPreferences userPreferences() {
return new UserPreferences();
}
@Bean
public Service userService() {
UserService service = new SimpleUserService();
// a reference to the proxied userPreferences bean
service.setUserPreferences(userPreferences());
return service;
}
自定義Bean命名
默認(rèn)情況下,配置類使用@Bean方法的名稱作為生成的Bean的名稱。但是,可以使用name屬性覆蓋此功能,如以下示例所示:
@Configuration
public class AppConfig {
@Bean("myThing")
public Thing thing() {
return new Thing();
}
}
Bean別名
正如Naming Beans中所討論的,有時需要為單個bean提供多個名稱,也稱為bean別名。@Bean注釋的name屬性接受用于此目的的String數(shù)組。以下示例顯示了如何為bean設(shè)置多個別名:
@Configuration
public class AppConfig {
@Bean({"dataSource", "subsystemA-dataSource", "subsystemB-dataSource"})
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
}
}
Bean描述
有時,提供一個bean的更詳細(xì)的文本描述是有幫助的。當(dāng)bean被公開(可能是通過JMX)用于監(jiān)視目的時,這可能特別有用。
要向@Bean添加描述,可以使用@Description注釋,如下例所示:
@Configuration
public class AppConfig {
@Bean
@Description("Provides a basic example of a bean")
public Thing thing() {
return new Thing();
}
}
完畢?。?!