spring @Bean注解的使用
@Bean 的用法
@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名
定义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 and @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的scope
使用@Scope注解
你能够使用@Scope注解来指定使用@Bean定义的bean
@Configuration
public class MyConfiguration { @Bean
@Scope("prototype")
public Encryptor encryptor() {
// ...
} }
@Scope and scoped-proxy
spring提供了scope的代理,可以设置@Scope的属性proxyMode来指定,默认是ScopedProxyMode.NO, 你可以指定为默认是ScopedProxyMode.INTERFACES或者默认是ScopedProxyMode.TARGET_CLASS。
以下是一个demo,好像用到了(没看懂这块)
// 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的命名
默认情况下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();
} }
spring @Bean注解的使用的更多相关文章
- spring @bean注解
1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...
- Spring @Bean 注解的使用
使用说明 这个注解主要用在方法上,声明当前方法体中包含了最终产生 bean 实例的逻辑,方法的返回值是一个 Bean.这个 bean 会被 Spring 加入到容器中进行管理,默认情况下 bean 的 ...
- Spring @Bean注解 (基于java的容器注解)
基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...
- spring @Bean注解解释
解释:java config配置一个重要注解 @Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法,并且交给Spring容器管理:从这 我们就明白了为啥@Bean是放在方法的注释上 ...
- Spring bean注解配置(1)
Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图 在使用注解方式配置bean时,需要引进一个包: 使用方法: 1.为需要使用注解方 ...
- spring @bean 的理解
1.spring @bean 注解只能注解到方法上 2. 该方法必须返回一个实例对象 3.该过程相当于,通过一个方法去构造一个实例对象 ,然后交给spring管理 4.使用场景 如需要构造出一个特 ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- Spring 框架 详解 (四)------IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...
- spring利用注解来注册bean到容器
1.spring利用注解来定义bean,或者利用注解来注册装配bean.包括注册到ioc中,装配包括成员变量的自动注入. 1.spring会自动扫描所有类的注解,扫描这些注解后,spring会将这些b ...
随机推荐
- win10刻录光盘失败,一直显示有准备好写入到光盘中的文件
这是因为前面刻录留下的缓存导致的 解决方法是,删除系统刻录缓存文件 刻录缓存路径大概在以下位置(其中ase那个地方需要修改,改成自己的登录用户账号名即可) C:\Users\ase\AppData\L ...
- defer、return、返回值,这三者的执行逻辑
defer.return.返回值,这三者的执行逻辑是: return 最先执行,return 负责将结果写入返回值中:接着defer执行,可能修改返回值:最后函数携带当前返回值退出.
- long和int互转
现在分long,Long,int,Integer互相转换,分8种情况 a , b long, int b=(int)a; long,Integer ...
- 【转】Linux环境进程间通信(五) 共享内存(上)
转自:https://www.ibm.com/developerworks/cn/linux/l-ipc/part5/index1.html 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以 ...
- markdown特殊符号语法
符号 说明 对应编码 & AND符号 & < 小于 < > 大于 > _ ...
- [UE4]Spline
Spline和Spline Mesh的区别: 1.Spline Mesh是有实体表现的,Spline Mesh可以拉伸弯曲实体模型,Spline Mesh是具象. 2.Spline 只有曲线,没有实体 ...
- 网页中HTML代码如何实现字体删除线效果
有的朋友在制作网站的时候,需要给字体制作删除线,例如:选择题,错误标识等!那么我们就需要用到了<s>这个标签写法如下 字体删除线: <s>这里是内容</s> 效果如 ...
- 转载 修改oracle用户密码永不过期
1.查看用户的proifle是哪个,一般是default: sql>SELECT username,PROFILE FROM dba_users; 2.查看指定概要文件(如default)的密码 ...
- Sping Cloud项目启动报A component required a bean of type 'com.tianyan.bbc.dao.SecurityUserBaseMapper' that could not be found.
项目构建正常,启动和Debug报以下错误: Error starting ApplicationContext. To display the conditions report re-run you ...
- 如何获取微信小店小程序的AppID
2017年11月16日,微信有一个重磅的宣布:为了帮商家在微信内快速开店,方便商家和用户之间沟通,所有认证公众号,可快速创建微信小店小程序.这个改变是否能给微信小店带来新的生机?还需要时间的考验.微信 ...