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 ...
随机推荐
- iOS Simulator 模拟器 与 Android Emulator 仿真器:为什么叫不同的英文名字?(待补充)
iOS Simulator 模拟器 与 Android Emulator 仿真器:为什么叫不同的英文名字?(待补充)
- Zabbix 3.4 服务端安装部署
关于zabbix的安装部署官方也提供了详细的安装文档,链接如下: https://www.zabbix.com/download 选择zabbix的版本,服务器平台及使用的数据库 安装和配置zabbi ...
- api签名认证方案
微信签名算法 token (自己后台配置) nonce:随机数 signature:签名 echostr:返回字符串 https://mp.weixin.qq.com/wiki?t=resource/ ...
- UML 资料整理
参考:http://www.uml.org.cn/oobject/201211231.asp 一.类的属性的表示方式 在UML类图中,类使用包含类名.属性(field) 和方法(method) 且带有 ...
- ThinkPHP 中使用 IS_AJAX 判断原生 JS 中的 Ajax 出现问题
问题: 在 ThinkPHP 中使用原生 js 发起 Ajax 请求的时候.在控制器无法使用 IS_AJAX 进行判断.而使用 jQuery 中的 ajax 是没有问题的. 在ThinkPHP中.有一 ...
- hive 的map数和reduce如何确定(转)
转自博客:https://blog.csdn.net/u013385925/article/details/78245011(没找到原创者,该博客也是转发) 一. 控制hive任务中的map ...
- RDLC报表系列--------报表分页显示标题
RDLC表格标题分页后,标题不显示网上的办法也不少.怎奈我试过很多,只有这个可以解决.留下代码以后使用 将报表以 XML的方式打开,搜索找到“详细信息” 在这个位置 报表分页标题设置<Tabli ...
- Indent Guides插件格式代码
vs开发工具实用性插件 一. 在开发遇到过很多括号缩近的,很是头疼,于是上网收了一下,希望对大家有所帮助. 第一款插件:Indent Guides 这款插件是给代码块增长对齐线,以标识匹配的花括号 ...
- HUE安装过程中的一些坑
1. gcc: error: krb5-config:: No such file or directory 执行安装krb5-devel yum provides krb5-config 得到提示: ...
- vim的基础操作