@Bean和@Scope
文章內容來自於:
https://www.cnblogs.com/feiyu127/p/7700090.html,作者:飛羽127
https://blog.csdn.net/tracycater/article/details/54019223,作者:羅羅諾亞-小魚
文章主要用於自己學習SpringBoot,方便以後的查詢
@Bean
是一個方法級別上的註解,主要用在@Configuration
和@Compoment
註解的類裏
定義Bean
下面是@Configuration
裏的一個例子
@Configuration
public class AppConfig {
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
這個配置等同於XML中的配置
<beans>
<bean id="transferService" class="com.acme.TransferServiceImpl"/>
</beans>
Bean的依賴
@Bean
也可以依賴其他任意數量的bean,如果TransferService 依賴 AccountRepository,我們可以通過方法參數實現這個依賴
@Configuration
public class AppConfig {
@Bean
public TransferService transferService(AccountRepository accountRepository) {
return new TransferServiceImpl(accountRepository);
}
}
接收生命週期的回調
任何使用@Bean
定義的bean,也可以執行生命週期的回調函數,類似@PostConstruct
和@PreDestroy
的方法
public class Foo {
public void init() {
// initialization logic
}
}
public class Bar {
public void cleanup() {
// destruction logic
}
}
@Configuration
public class AppConfig {
@Bean(initMethod = "init")
public Foo foo() {
return new Foo();
}
@Bean(destroyMethod = "cleanup")
public Bar bar() {
return new Bar();
}
}
默認使用javaConfig
配置的bean
,如果存在close
或者shutdown
方法,則在bean
銷燬時會自動執行該方法,如果你不想執行該方法,則添加@Bean(destroyMethod="")
來防止出發銷燬方法
自定義Bean的命名
默認情況下bean的名稱和方法名稱相同,你也可以使用name屬性來指定
@Configuration
public class AppConfig {
@Bean(name = "myFoo")
public Foo foo() {
return new Foo();
}
}
Bean的別名
bean的命名支持別名,使用方法如下
@Configuration
public class AppConfig {
@Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
public DataSource dataSource() {
// instantiate, configure and return DataSource bean...
}
}
Bean的描述
有時候提供bean的詳細信息也是很有用的,bean的描述可以使用 @Description來提供
@Configuration
public class AppConfig {
@Bean
@Description("Provides a basic example of a bean")
public Foo foo() {
return new Foo();
}
}
指定Bean的Scope
你能夠使用@Scope
註解來指定使用@Bean
定義的bean
@Configuration
public class MyConfiguration {
@Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
}
}
相當於在xml中,配置如下
<!-- 具體的作用域需要在 scope 屬性中定義 -->
<bean id="XXX" class="com.XXX.XXXXX" scope="XXXX" />
scope
也稱作用域,在 Spring IoC 容器是指其創建的 Bean 對象相對於其他 Bean 對象的請求可見範圍。在 Spring IoC 容器中具有以下幾種作用域:基本作用域(singleton、prototype)
,Web 作用域(reqeust、session、globalsession)
,自定義作用域
。
singleton:單例模式,在整個Spring IoC容器中,使用singleton定義的Bean將只有一個實例
prototype:原型模式,每次通過容器的getBean方法獲取prototype定義的Bean時,都將產生一個新的Bean實例
request:對於每次HTTP請求,使用request定義的Bean都將產生一個新實例,即每次HTTP請求將會產生不同的Bean實例。只有在Web應用中使用Spring時,該作用域纔有效
session:對於每次HTTP Session,使用session定義的Bean豆漿產生一個新實例。同樣只有在Web應用中使用Spring時,該作用域纔有效
globalsession:每個全局的HTTP Session,使用session定義的Bean都將產生一個新實例。典型情況下,僅在使用portlet context的時候有效。同樣只有在Web應用中使用Spring時,該作用域纔有效
其中比較常用的是singleton和prototype兩種作用域。對於singleton作用域的Bean,每次請求該Bean都將獲得相同的實例。容器負責跟蹤Bean實例的狀態,負責維護Bean實例的生命週期行爲;如果一個Bean被設置成prototype作用域,程序每次請求該id的Bean,Spring都會新建一個Bean實例,然後返回給程序。在這種情況下,Spring容器僅僅使用new 關鍵字創建Bean實例,一旦創建成功,容器不在跟蹤實例,也不會維護Bean實例的狀態。
如果不指定Bean的作用域,Spring默認使用singleton作用域。Java在創建Java實例時,需要進行內存申請;銷燬實例時,需要完成垃圾回收,這些工作都會導致系統開銷的增加。因此,prototype作用域Bean的創建、銷燬代價比較大。而singleton作用域的Bean實例一旦創建成功,可以重複使用。因此,除非必要,否則儘量避免將Bean被設置成prototype作用域。
基於註解開發時,@scope完成bean的作用域配置默認是單例模式(singleton)如果需要設置的話可以修改對應值與以上提到的一致例如:@scope(“prototype”)