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 ...
随机推荐
- create-react-app源码解读之为什么不搞个山寨版的create-react-app呢?
最近把 vue-cli@2.x 和 create-react-app 的源码都看了一遍.由于现在官方推荐使用 vue-cli@3.0 ,改动比较大,所以就不写关于 vue-cli 的了(据说是因为 v ...
- 西门子S7-200SMART PLC视频教程(百度网盘)
西门子S7-200SMART PLC视频教程(百度网盘)西门子S7-200 SMART PLC是西门子公司推出的高性价比小型plc,是国内广泛使用的S7-200PLC的更新换代产品. 以下是关于S7- ...
- tomcat 发布后中文乱码问题
接口收到数据,使用Eclipse运行调试中文正常显示,发布到Tomcat后中文出现乱码情况: 解决方法: tomcat启动时默认使用系统编码,可更改tomcat bin目录下catalina.bat文 ...
- node.js 远程调试debug产线环境代码
一.背景: 产线机器出bug,不能重启服务,需要保留现场,问题不好排查,只能靠远程debug. 二.实现步骤 1. 登录远程机器执行如下命令,nodePid为node服务的pid kill -usr1 ...
- MegaCLi命令总结
MegaCli命令总结 MegaCli 版本8.00.29,raid卡为lsi 8888elp,固件11.0.1-0036 1 巡读 一MegaCli -adppr -enblauto -a0 ...
- 浅谈SPI总线
SPI总线概述 SPI全称是串行外设接口(Serial Peripheral Interface),是由Motorola提出的一种全双工同步串行通信接口,通信波特率可以高达5Mbps,但具体速 ...
- Vmware虚拟中克隆主机没IP地址?怎么解决?
Vmware虚拟中克隆主机没IP地址?怎么解决? 修改网卡的配置文件: 清空如下的文件: 重启主机即可!
- Dijkstra求最短路径&例题
讲了半天好像也许maybe听懂了一点,先写下来233 先整理整理怎么存(开始绕) 最简单的是邻接矩阵存,但是开到10000*10000就MLE了,所以我们用链式前向星存(据说是叫这个名字吧) 这是个什 ...
- 配置中心Nacos
Nacos 是阿里巴巴2018年7月份开源的项目,如其名, Naming Configuration Service ,专注于服务发现和配置管理领域. Nacos 是什么?上面已经大概介绍了,更多详细 ...
- [UE4]在Character中使用Add Spline Mesh Component,关于Transform.Mobility
一.因为Character是可移动的,因此也需要把Add Spline Mesh Component的Transform.Mobility设置为Movable 二.不然就会得到类似这样的提示.错误信息 ...