@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注解的使用的更多相关文章

  1. spring @bean注解

    1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...

  2. Spring @Bean 注解的使用

    使用说明 这个注解主要用在方法上,声明当前方法体中包含了最终产生 bean 实例的逻辑,方法的返回值是一个 Bean.这个 bean 会被 Spring 加入到容器中进行管理,默认情况下 bean 的 ...

  3. Spring @Bean注解 (基于java的容器注解)

    基于java的容器注解,意思就是使用Java代码以及一些注解,就可以取代spring 的 xml配置文件. 1-@Configuration & @Bean的配合 @Configuration ...

  4. spring @Bean注解解释

    解释:java config配置一个重要注解 @Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法,并且交给Spring容器管理:从这   我们就明白了为啥@Bean是放在方法的注释上 ...

  5. Spring bean注解配置(1)

    Spring自带的@Component注解及扩展@Repository.@Service.@Controller,如图 在使用注解方式配置bean时,需要引进一个包: 使用方法: 1.为需要使用注解方 ...

  6. spring @bean 的理解

    1.spring @bean 注解只能注解到方法上 2. 该方法必须返回一个实例对象 3.该过程相当于,通过一个方法去构造一个实例对象 ,然后交给spring管理 4.使用场景   如需要构造出一个特 ...

  7. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  8. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  9. spring利用注解来注册bean到容器

    1.spring利用注解来定义bean,或者利用注解来注册装配bean.包括注册到ioc中,装配包括成员变量的自动注入. 1.spring会自动扫描所有类的注解,扫描这些注解后,spring会将这些b ...

随机推荐

  1. 使用 opendistro for elasticsearch 做为graylog的后端存储

    graylog 是一个很不错的日志分析.收集.报警平台,包好了丰富的插件,同时内部的架构设计很不错 input 组件很多,使用stream.pipeline可以方便的进行数据处理,可以同时3.0 对于 ...

  2. Python(四) —— 函数

    什么是函数? 把一堆代码放一起就叫函数 函数用来干什么? 不复写代码,提高代码重复利用程度 怎么定义以及调用函数: def fun1(): #定义函数 print('这是一个函数') #函数体,里面什 ...

  3. jQuery基础(四)动画

    1.动画基础隐藏和显示 jQuery中隐藏元素的hide方法 $elem.hide() 提供参数: .hide( options ) 当提供hide方法一个参数时,.hide()就会成为一个动画方法. ...

  4. Application、QueryString、session、cookie、ViewState、Server.Transfer等

    Application: WebForm1.aspx: protected void Button1_Click(object sender, EventArgs e) { ; Response.Re ...

  5. C# 生成海报,文本区域指定和换行,图片合成

    protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string path = Server.MapPa ...

  6. JS:Math 对象方法

    Math 对象方法方法     描述Math.ceil(x)     对数进行上舍入.(向上取整:大于等于x的最小整数)Math.floor(x)     对数进行下舍入.(小于等于x的最大整数)Ma ...

  7. [ZZ] matlab中小波变换函数dwt2和wavedec2 系数提取函数appcoef2和detcoef2

    https://zhidao.baidu.com/question/88038464.html DWT2是二维单尺度小波变换,其可以通过指定小波或者分解滤波器进行二维单尺度小波分解. 而WAVEDEC ...

  8. 【环境部署】centos7安装mysql-5.7.19 group-replication

    --mysql高可用官方文档: https://dev.mysql.com/doc/refman/5.7/en/group-replication.html mysql下载地址: https://ww ...

  9. 【java】之位运算^,&,<<,>>,<<<,>>>总结

    1.^(亦或运算) ,针对二进制,相同的为0,不同的为1 public static void main(String[] args) { System.out.println("2^3运算 ...

  10. [UE4]Spline Mesh Actor

    作用: 1.按照Spline设置的轨迹,进行显示网格物体. 曲线模型 一.赋值Static Mesh,任意的Static Mesh都可以. 二.调整方向 三.Spline Mesh Actor有2个端 ...