转自;http://www.cnblogs.com/feiyu127/p/7700090.html

@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(); } }

转载于:https://www.cnblogs.com/sharpest/p/7748627.html

spring @Bean注解的使用相关推荐

  1. 被各种注解搞晕了?那快来看看Spring Bean注解详解!

    前言 本篇博客中,我们将会讨论用于声明不同类型 Beans 的几种最常用的 Spring Bean 注解. 众所周知,Spring 容器中有许多配置 Bean 的方法,我们既可以通过 XML 配置,也 ...

  2. spring @bean注解

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

  3. Spring - Bean注解配置光速入门

    Bean注解配置光速入门 步骤一: 创建 web 项目,引入 Spring 的开发包 在 Spring 的注解的 AOP 中需要引入 spring-aop 的 jar 包 步骤二: 引入相关配置文件 ...

  4. Spring : @Bean注解

    1.美图 2.概述 @Bean是一个方法级别上的注解,主要用在@SpringBootConfiguration.@Configuration注解的配置类里面使用.Spring Boot会帮助把@Bea ...

  5. 一次性讲清 Spring 常用注解 @Bean 、 @Component 、@Autowire、@Resource 的区别, 你知道吗?

    本文打算介绍几个不太容易说出其区别,或者用途的 Spring 注解,比如 @Component 与 @Bean 的比较,@ControllerAdvice 是如何处理自定义异常的等等. Spring ...

  6. Spring bean注入之注解注入-- @Autowired原理

    之前我们已经讲述过bean注入是什么了,也使用了xml的配置文件进行bean注入,这也是Spring的最原始的注入方式(xml注入). 本节课就讲注解注入. 主要讲解的注解有以下几个: @Autowi ...

  7. spring系列-注解驱动原理及源码-bean生命周期

    目录 一.Bean的初始化和销毁 1.@Bean指定初始化和销毁方法 2.通过实现InitializingBean和Disposabean来初始化和销毁bean 3.使用JSR250定义的@PostC ...

  8. spring系列-注解驱动原理及源码-bean组件注册

    目录 一.环境初始化 1.环境准备 二.bean的手动注入 1.xml方式注入bean 2.使用@Configuration&@Bean方式注入bean 三.自动扫描注册组件及bean 1.使 ...

  9. spring的注解开发@Component @Bean @Value @Autowired、@Qualifier @PropertySource @Configuration

    spring的注解开发 启动注解功能 启动注解功能 ⚫ 启动注解扫描,加载类中配置的注解项 ⚫ 说明: ◆ 在进行包所扫描时,会对配置的包及其子包中所有文件进行扫描 ◆ 扫描过程是以文件夹递归迭代的形 ...

  10. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

最新文章

  1. [导入]ASP.NET AJAX 说明文档-客户端引用-全局命名空间-JavaScript 基础类型扩展-Array 类型扩展-add 函数...
  2. 判断三角形java代码_小猿圈Java循环嵌套语法的使用介绍
  3. java+自定义异常类+输入若干成绩 为负数时输出抛出异常_java+自定义异常类+输入若干成绩 为...
  4. 转:小城也有好去处(3):云南建水 - 丽江大理之后第三城
  5. jar打包 剔除第三方依赖以及它的依赖_maven打包成第三方jar包且把pom依赖包打入进来的方法...
  6. Sublime3 快捷键
  7. android开源tabview,TabBarView
  8. Java开发中的基本数据类型有哪些?
  9. 【图像隐写】基于matlab LDPC编码译码改进DCT水印嵌入提取【含Matlab源码 832期】
  10. BZOJ1090[SCOI2003] 字符串折叠
  11. python文本模糊匹配
  12. Si24R2E超低功耗2.4G有源RFID芯片
  13. app invento2r wxbit版 快速开发简单蓝牙上位/蓝牙串口助手
  14. 中软国际软件测试培训中心,中软国际准员工培养计划C++开发/软件测试方向开班典礼...
  15. 洛谷P4158 [SCOI2009]粉刷匠 题解
  16. error: Failed dependencies: perl(Data::Dumper) is needed by MySQL-server-5.6.46-1.el7.x8
  17. java pdf添加页码_java itext pdf 肿么加页码
  18. javascript 中 caller 与 callee 的作用以及用法
  19. CST微波工作室学习笔记—10.数据后处理
  20. 《编译 - 编译杂记》GCC优化等级说明

热门文章

  1. Atitit 避税之道 如何降低企业与项目组成本 attilax总结
  2. Atitit.会员卡(包括银行卡)api的设计
  3. Atitit.log日志技术的最佳实践attilax总结
  4. Atitit. Xss 漏洞的原理and应用xss木马
  5. paip.python php的未来预测以及它们的比较优缺点
  6. paip.php 与js 的相似性以及为什么它们这么烂还很流行。。
  7. paip.提升开发效率-----vs2010 快速查找文件
  8. paip.跨平台跨语言自定义加密方法
  9. 专访凯美瑞德研发总监孟江华:银行间市场操作系统的自主可控亟待加强
  10. ZStack 3.6.0发布:支持云主机从KVM云平台在线迁移至ZStack