@Bean的用法

@Bean 的用法

@Bean是一個方法級別上的註解,主要用在@Configuration註解的類裏也可以用在@Component註解的類裏。添加的bean的id爲方法名

定義bean

下面是@Configuration裏的一個例子

  1. @Configuration
  2. public class AppConfig {
  3. @Bean
  4. public TransferService transferService() {
  5. return new TransferServiceImpl();
  6. }
  7. }

這個配置就等同於之前在xml裏的配置

  1. <beans>
  2. <bean id="transferService" class="com.acme.TransferServiceImpl"/>
  3. </beans>

bean的依賴

@bean 也可以依賴其他任意數量的bean,如果TransferService 依賴 AccountRepository,我們可以通過方法參數實現這個依賴

  1. @Configuration
  2. public class AppConfig {
  3. @Bean
  4. public TransferService transferService(AccountRepository accountRepository) {
  5. return new TransferServiceImpl(accountRepository);
  6. }
  7. }

接受生命週期的回調

任何使用@Bean定義的bean,也可以執行生命週期的回調函數,類似@PostConstruct and @PreDestroy的方法。用法如下

  1. public class Foo {
  2. public void init() {
  3. // initialization logic
  4. }
  5. }
  6. public class Bar {
  7. public void cleanup() {
  8. // destruction logic
  9. }
  10. }
  11. @Configuration
  12. public class AppConfig {
  13. @Bean(initMethod = "init")
  14. public Foo foo() {
  15. return new Foo();
  16. }
  17. @Bean(destroyMethod = "cleanup")
  18. public Bar bar() {
  19. return new Bar();
  20. }
  21. }

默認使用javaConfig配置的bean,如果存在close或者shutdown方法,則在bean銷燬時會自動執行該方法,如果你不想執行該方法,則添加@Bean(destroyMethod="")來防止出發銷燬方法

指定bean的scope

使用@Scope註解

你能夠使用@Scope註解來指定使用@Bean定義的bean

  1. @Configuration
  2. public class MyConfiguration {
  3. @Bean
  4. @Scope("prototype")
  5. public Encryptor encryptor() {
  6. // ...
  7. }
  8. }

@Scope and scoped-proxy

spring提供了scope的代理,可以設置@Scope的屬性proxyMode來指定,默認是ScopedProxyMode.NO, 你可以指定爲默認是ScopedProxyMode.INTERFACES或者默認是ScopedProxyMode.TARGET_CLASS。
以下是一個demo,好像用到了(沒看懂這塊)

  1. // an HTTP Session-scoped bean exposed as a proxy
  2. @Bean
  3. @SessionScope
  4. public UserPreferences userPreferences() {
  5. return new UserPreferences();
  6. }
  7. @Bean
  8. public Service userService() {
  9. UserService service = new SimpleUserService();
  10. // a reference to the proxied userPreferences bean
  11. service.setUserPreferences(userPreferences());
  12. return service;
  13. }

自定義bean的命名

默認情況下bean的名稱和方法名稱相同,你也可以使用name屬性來指定

  1. @Configuration
  2. public class AppConfig {
  3. @Bean(name = "myFoo")
  4. public Foo foo() {
  5. return new Foo();
  6. }
  7. }

bean的別名

bean的命名支持別名,使用方法如下

  1. @Configuration
  2. public class AppConfig {
  3. @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
  4. public DataSource dataSource() {
  5. // instantiate, configure and return DataSource bean...
  6. }
  7. }

bean的描述

有時候提供bean的詳細信息也是很有用的,bean的描述可以使用 @Description來提供

  1. @Configuration
  2. public class AppConfig {
  3. @Bean
  4. @Description("Provides a basic example of a bean")
  5. public Foo foo() {
  6. return new Foo();
  7. }
  8. }
				<script>
					(function(){
						function setArticleH(btnReadmore,posi){
							var winH = $(window).height();
							var articleBox = $("div.article_content");
							var artH = articleBox.height();
							if(artH > winH*posi){
								articleBox.css({
									'height':winH*posi+'px',
									'overflow':'hidden'
								})
								btnReadmore.click(function(){
									if(typeof window.localStorage === "object" && typeof window.csdn.anonymousUserLimit === "object"){
										if(!window.csdn.anonymousUserLimit.judgment()){
											window.csdn.anonymousUserLimit.Jumplogin();
											return false;
										}else if(!currentUserName){
											window.csdn.anonymousUserLimit.updata();
										}
									}
									
									articleBox.removeAttr("style");
									$(this).parent().remove();
								})
							}else{
								btnReadmore.parent().remove();
							}
						}
						var btnReadmore = $("#btn-readmore");
						if(btnReadmore.length>0){
							if(currentUserName){
								setArticleH(btnReadmore,3);
							}else{
								setArticleH(btnReadmore,1.2);
							}
						}
					})()
				</script>
				</article>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章