@Bean 的用法

摘要:
@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。

@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名

定义bean

下面是@Configuration里的一个例子

1 @Configuration
2 public classAppConfig {
3 
4 @Bean
5     publicTransferService transferService() {
6         return newTransferServiceImpl();
7 }
8 }

这个配置就等同于之前在xml里的配置

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

bean的依赖

@bean 也可以依赖其他任意数量的bean,如果TransferService 依赖 AccountRepository,我们可以通过方法参数实现这个依赖

1 @Configuration
2 public classAppConfig {
3 @Bean
4     publicTransferService transferService(AccountRepository accountRepository) {
5         return newTransferServiceImpl(accountRepository);
6 }
7 }

接受生命周期的回调

任何使用@Bean定义的bean,也可以执行生命周期的回调函数,类似@PostConstruct and @PreDestroy的方法。用法如下

1 public classFoo {
2     public voidinit() {
3         //initialization logic
4 }
5 }
6 
7 public classBar {
8     public voidcleanup() {
9         //destruction logic
10 }
11 }
12 
13 @Configuration
14 public classAppConfig {
15 
16     @Bean(initMethod = "init")
17     publicFoo foo() {
18         return newFoo();
19 }
20 
21     @Bean(destroyMethod = "cleanup")
22     publicBar bar() {
23         return newBar();
24 }
25 }

默认使用javaConfig配置的bean,如果存在close或者shutdown方法,则在bean销毁时会自动执行该方法,如果你不想执行该方法,则添加@Bean(destroyMethod="")来防止出发销毁方法

指定bean的scope

使用@Scope注解

你能够使用@Scope注解来指定使用@Bean定义的bean

1 @Configuration
2 public classMyConfiguration {
3 
4 @Bean
5     @Scope("prototype")
6     publicEncryptor encryptor() {
7         //...
8 }
9 }

自定义bean的命名

默认情况下bean的名称和方法名称相同,你也可以使用name属性来指定

1 @Configuration
2 public classAppConfig {
3 
4     @Bean(name = "myFoo")
5     publicFoo foo() {
6         return newFoo();
7 }
8 }

bean的别名

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

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

bean的描述

有时候提供bean的详细信息也是很有用的,bean的描述可以使用 @Description来提供

1 @Configuration
2 public classAppConfig {
3 
4 @Bean
5     @Description("Provides a basic example of a bean")
6     publicFoo foo() {
7         return newFoo();
8 }
9 }

免责声明:文章转载自《@Bean 的用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ASP.NET Core 模型验证的一个小小坑基于AngularJs的上传控件-angular-file-upload下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SpringBoot启动过程中涉及到了扩展接口

SpringApplicationRunListener接口 1、ApplicationListener接口 是ApplicationContext的事件监听器 2、EnvironmentPostProcessor接口 上下文环境后置处理器,事件中调用 3、PropertySourceLoader接口 自定义配置文件加载器,自己解析配置文件属性...

spring中scope的prototype与singleton区别

最近在研究单例模式,突然想起项目中以下配置,scope="singleton" 和 scope="prototype"到底有何区别呢?以下做下简要分析。 <bean class="com.****.boss.domain.utils.CacheManager" scope="singleton" init-method="init" destr...

关联查询报错org.apache.ibatis.builder.BuilderException: Ambiguous collection type for property 'episodeList'. You must specify 'javaType' or 'resultMap'.

三表关联查询报错, 单表不报错, 之前写过一遍没报错,后来查出问题所在了,原来是insert语句多写了半格括号"(" 这个问题查了很久很久... 报错信息如下 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'vi...

Spring框架第一天(搭建项目)

Spring框架 1.简介 1.1 Spring是什么 一个开源的框架,是JavaEE开源框架 Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以IoC(Inverse Of Control:反转控制)和AOP(Aspect Oriented Programming:面向切面编程)为内核。 提供了展现层 Spring...

MultipartFile(文件的上传)--CommonsMultipartResolver

一 : applicationContext.xml中:必须声明不然获取不到 <!-- 上传文件的配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">...

Spring:No bean named 'beanScope' is defined

初学Spring,“No bean named 'beanScope' is defined”这个问题困扰了我好几个小时,查资料无果后,重写好几遍代码后发现问题居然是配置文件不能放在包里。。。要放在src的直接目录下。。。心碎了一地。。。 使用的是  windows 10 / eclipse 4.5.2 /Spring-framework-4.3.0/ 下...