@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

【Bean的Scope】
Scope 描述的是Spring 容器如何新建Bean 的实例的。Spring 的Scope 有以下几种,通过@Scope 注解来实现。

1. Singleton :一个Spring 容器中只有一个Bean 的实例,此为Spring 的默认配置,全容器共享一个实例。
2. Prototype :每次调用新建一个Bean 的实例。
3. Request: Web 项目中,给每一个http request 新建一个Bean 实例。
4. Session: Web 项目中,给每一个http session 新建一个Bean 实例。
5. Global Session :这个只在portal 应用中有用,给每一个global http session 新建一个Bean
实例。
6. 在Spring Batch 中还有一个Scope 是使用@StepScope

如果希望在启动的时候加载以后不在加载,使用单例模式,就是

@Scope("Singleton")  或者 @Scope,因为默认情况下就是 单例的;

@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注解的使用
参考
04-SpringBoot——Spring常用配置-Bean的Scope

Spring标签之Bean @Scope的更多相关文章

  1. spring 支持集中 bean scope?

    Spring bean 支持 5 种 scope: Singleton - 每个 Spring IoC 容器仅有一个单实例. Prototype - 每次请求都会产生一个新的实例. Request - ...

  2. Spring学习(二):Spring支持的5种Bean Scope

    序言 Scope是定义Spring如何创建bean的实例的.Spring容器最初提供了两种bean的scope类型:singleton和prototype,但发布2.0以后,又引入了另外三种scope ...

  3. spring中的bean的属性scope

    spring中bean的scope属性,有如下5种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例 prototype表示每次获得be ...

  4. Spring中<bean>标签之使用p标签配置bean的属性

    在spring的bean配置文件中我们常可以见到下面的例子: <bean id="user" class="com.sys.User" p:name-re ...

  5. Spring bean - scope详解

    Scope是定义Spring如何创建bean的实例的. 在创建bean的时候可以带上scope属性,scope有下面几种类型. Singleton 这也是Spring默认的scope,表示Spring ...

  6. Spring Bean Scope (作用域)

    singleton: 单例模式,针对每个spring容器,只有一个该类的实例被管理,每次调用此实例都是同一个对象被返回,所以适用于无状态bean.默认情况下,singleton作为spring容器中b ...

  7. Spring学习(15)--- 基于Java类的配置Bean 之 @Bean & @Scope 注解

    默认@Bean是单例的,但可以使用@Scope注解来覆盖此如下: @Configuration public class MyConfiguration { @Bean @Scope("pr ...

  8. [spring]03_装配Bean

    3.1 JavaBean 3.1.1 JavaBean 是什么 JavaBean 是一种JAVA语言写成的可重用组件. 为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器. Jav ...

  9. Spring系列之bean的使用

    一.Bean的定义 <bean id="userDao" class="com.dev.spring.simple.MemoryUserDao"/> ...

随机推荐

  1. 第1章 从开机加电到main函数之前的过程

    主要讲解了80x86cpu在启动的时候时bios如何工作,以及如何最终转换到保护模式. 1.1 启动bios 80x86作为冯诺依曼结构下的cpu,工作模式也是取指执行,即cpu根据cs:ip寄存器的 ...

  2. EL表达式运算符使用

    EL表达式关系运算符的使用 == eq 等于 != ne 不等于 > gt 大于 < lt 小于 >= ge 大于等于 <= le 小于等于 举例说明 > 或者 gt, ...

  3. Python:Day07 作业

    三级菜单: 自己做的代码: china = { '江苏':{ '南京':{ '江宁':{}, '白下':{}, '栖霞':{}, '江淮':{}, '浦口':{} }, '宿迁':{ '宿城区':{} ...

  4. 2-jsp简介

    一.什么是jsp:1.只能运行在服务器中2.可以将java代码嵌入html页面中的技术 补充:在eclipse中修改jsp的创建模板 window--preference--web--jsp file ...

  5. Memcache分布式锁 转发 https://www.cnblogs.com/li150dan/p/9529090.html

    在分布式缓存的应用中,会遇到多个客户端同时争用的问题.这个时候,需要用到分布式锁,得到锁的客户端才有操作权限 下面通过一个简单例子介绍: 这里引用的是Memcached.ClientLibrary.d ...

  6. 视觉机器学习------KNN学习

    KNN(K-Nearest Neighbor algorithm, K最近邻方法)是一种统计分类器,属于惰性学习. 基本思想:输入没有标签即未经分类的新数据,首先提取新数据的特征并与测试集中的每一个数 ...

  7. RestFul风格API(Swagger)--从零开始Swagger

    引言:随着技术的革新,现在的系统基本上都是前后端分离,并且在各自的道路上越走越远,而前后端之间通信或者联系的桥梁就是API,而这里基于RESTful风格的API框架就来了!欲知后事如何,客官别急,往下 ...

  8. 【重磅干货整理】机器学习(Machine Learning)与深度学习(Deep Learning)资料汇总

    [重磅干货整理]机器学习(Machine Learning)与深度学习(Deep Learning)资料汇总 .

  9. 性能调优8:分组聚合 - group by

    聚合实际上对数据做分组统计,SQL Server使用两种操作符来实现聚合,流聚合(Stream Aggregation)和哈希聚合(Hash aggration).流聚合是非阻塞性的,具有流的特性,流 ...

  10. (1)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- 什么是微服务架构,.netCore微服务选型

    开发工具:VS2017 .Net Core 2.1 什么是微服务?单体结构: 缺点: 1)只能采用同一种技术,很难用不同的语言或者语言不同版本开发不同模块: 2)系统耦合性强,一旦其中一个模块有问题, ...