@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>

@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. }

猜你喜欢

转载自blog.csdn.net/yongwa123/article/details/84640708
今日推荐