@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() {
// ...
}
}
自定义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();
}
}
@Bean 的用法的更多相关文章
- SPRING IN ACTION 第4版笔记-第二章-003-以Java形式注入Bean、@Bean的用法
1. package soundsystem; import org.springframework.context.annotation.Bean; import org.springframewo ...
- java bean、List、数组、map和Json的相互转化
工程 json包为 代码 package com.my.json; public class ChildBean { private String childName; private String ...
- struts1 logic:iterate bean:write标签使用
只是截取项目中部分代码,供参考及日后查阅 用struts1标签html:select 展现select下拉列表 刚开始为如下代码: <html:select name="Shuiwuj ...
- spring @Bean注解的使用
@Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...
- Spring标签之Bean @Scope
@Bean 的用法 @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里.添加的bean的id为方法名 定义bean 下面是@Co ...
- SpringIOC 二—— 容器 和 Bean的深入理解
上文:Spring IOC 一--容器装配Bean的简单使用 上篇文章介绍了 Spring IOC 中最重要的两个概念--容器和Bean,以及如何使用 Spring 容器装配Bean.本文接着记录 S ...
- Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】
自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器中注册了,要怎么去区分对哪一个Bean进行注入呢? 如下情况, ...
- Eclipse/JavaWeb (三)三大框架之Spring框架 持续更新中...
(一)发展历史 现在我们有三个层了,可是每层之间的调用是怎样的呢?比如显示层的struts需要调用一个业务类,就需要new一个业务类出来,然后使用:业务层需要调用持久层的类,也需要new一个持久层类出 ...
- 01Spring_基本jia包的导入andSpring的整体架构and怎么加入日志功能
1.什么是Spring : v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:u ...
随机推荐
- MYSQL-8.0.11-WINX64(免安装版)配置
1. 解压zip包到安装目录 首先,将mysql-8.0.11-winx64.zip 解压缩到 安装D:/mysql-8.0.11-winx64 目录下, 2.配置文件 在安装根目录下添加 my.in ...
- RabbitMQ 的安装----windows环境
一.RabbitMQ在windows下的安装 RabbitMQ 它依赖于Erlang,在window上安装时,需要先安装Erlang. 首先确定你的window电脑是32位还是64位,然后下载对应版本 ...
- 深入path类
Path类放在System.IO命名空间里.Path是一个只包含有静态方法的实例类,所以它不需要在使用之前实例化.它有各种方法用于处理文件的扩展名.文件名.根路径,以及和更多和路径相关的方面.下面就是 ...
- 学号 20175223 《Java程序设计》第2周学习总结
学号 20175223 <Java程序设计>第2周学习总结 教材学习内容总结 第二章要点: 要点1:标识符与关键字 要点2:基本数据类型:逻辑类型boolean,整数类型int|byte| ...
- Unity动画机制 Animator与Animator Controller教程
Unity动画机制Animator 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- ...
- CentOS6.5安装mysql5.7
CentOS6.5安装mysql5.7 查看mysql的安装路径: [root@bogon ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql ...
- 【java多线程】队列系统之说说队列Queue
转载:http://benjaminwhx.com/2018/05/05/%E8%AF%B4%E8%AF%B4%E9%98%9F%E5%88%97Queue/ 1.简介 Queue(队列):一种特殊的 ...
- Makefile中的ifeq 多条件使用
Makefile中的ifeq 多条件使用 网上关于makefile中ifeq的介绍已经很多了,为什么我还要在写这篇文章,因为他们只说了if else两种条件的情况,并没有讲多于两种条件情况的使用. 多 ...
- Excel转datatable
如果想支持 .xls,.xlsx 两种格式 则必须安装一个exe文件,下载路径https://www.microsoft.com/zh-CN/download/details.aspx?id=1325 ...
- js实现表单
<html> <head> <title>表单页面</title> <meta http-equiv="Content- ...