1. Spring中bean的多种作用域

在默认情况下,Spring应用上下文中所有的bean都是以单例(singleton)的形式创建的,即不管给定的一个bean被注入到其他bean多少次,每次所注入的都是同一个实例。

Spring定义了多种作用域,可以基于这些作用域创建bean:

  1. 单例(Singleton):在整个应用中,只创建bean的一个实例。
  2. 原型(Prototype):每次注入或者通过Spring应用上下文获取的时候,都会创建一个新的bean实例。
  3. 会话(Session):在Web应用中,为每个会话创建一个bean实例。
  4. 请求(Request):在Web应用中,为每个请求创建一个bean实例。

单例是默认的作用域,如果要使用其它的作用域创建bean,需要使用@Scope注解,该注解可以和@Component@Service@Bean等注解一起使用。

2. 示例

为了更好的理解,我们通过具体的代码示例来理解下Singleton与ProtoType的区别。

2.1 新建Singleton类型的bean

package chapter03.scope;

import org.springframework.stereotype.Service;

@Service
public class DemoSingletonService {
}

因为Spring默认配置的Scope是Singleton,因此以上代码等价于以下代码:

package chapter03.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class DemoSingletonService {
}

上面的代码也可以写成@Scope("singleton")

2.2 新建ProtoType类型的bean

如果是自动装配bean,语法为:

package chapter03.scope;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DemoPrototypeService {
}

上面的代码也可以写成@Scope("prototype")

如果是在Java配置类中声明bean,语法为:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public DemoPrototypeService demoPrototypeService() {
return new DemoPrototypeService();
}

如果是在xml中声明bean,语法为:

<bean id="demoPrototypeService" class="chapter03.scope.DemoPrototypeService"
scope="prototype"/>

2.3 新建配置类

package chapter03.scope;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("chapter03.scope")
public class ScopeConfig {
}

2.4 测试

新建一个Main类,在main()方法中,分别从Spring容器中获取2次Bean,然后判断Bean的实例是否相等:

package chapter03.scope;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScopeConfig.class); DemoSingletonService s1 = context.getBean(DemoSingletonService.class);
DemoSingletonService s2 = context.getBean(DemoSingletonService.class); DemoPrototypeService p1 = context.getBean(DemoPrototypeService.class);
DemoPrototypeService p2 = context.getBean(DemoPrototypeService.class); System.out.println("s1 与 s2 是否相等:" + s1.equals(s2));
System.out.println("p1 与 p2 是否相等:" + p1.equals(p2)); context.close();
}
}

运行结果如下:

从运行结果也可以看出,默认情况下即Singleton类型的Bean,不管调用多少次,只会创建一个实例。

3. 注意事项

Singleton类型的Bean,@Scope注解的值必须是:singleton。

ProtoType类型的Bean,@Scope注解的值必须是:prototype。

即使是大小写不一致也不行,如我们将DemoPrototypeService类的代码修改为:

package chapter03.scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; @Service
@Scope("PROTOTYPE")
public class DemoPrototypeService {
}

此时运行代码,就会报错:

4. 源码及参考

源码地址:https://github.com/zwwhnly/spring-action.git,欢迎下载。

汪云飞《Java EE开发的颠覆者:Spring Boot实战》

原创不易,如果觉得文章能学到东西的话,欢迎点个赞、评个论、关个注,这是我坚持写作的最大动力。

如果有兴趣,欢迎添加我的微信:zwwhnly,等你来聊技术、职场、工作等话题(PS:我是一名奋斗在上海的程序员)。

最新文章

  1. uiautomator-----UiWatcher监听器
  2. 自己动手模拟开发一个简单的Web服务器
  3. C# OOP 重要部分全解
  4. IOS 6和 IOS7适配的一些问题
  5. mysql replication之binlog-do-db、binlog-ignore-db
  6. Lotus 迁移到Exchange 2010 之准备使用Transport 同步Lotus 相关信息!
  7. Linux下的memset函数
  8. SpringMVC04controller中定义多个方法
  9. glusterfs快速安装
  10. 微服务架构的简单实现-Stardust
  11. 基于FPGA的VGA显示静态图片
  12. 搭建Hadoop集群(centos6.7+hadoop-2.7.3)
  13. kubernetes之flannel
  14. Docker学习之2——镜像
  15. Django单表操作
  16. php 禁止屏蔽类
  17. qhfl-4 注册-登录-认证
  18. IP基本原理
  19. log4j下载地址及日志文件输入位置配置
  20. QQ检测登陆及QQ协议

热门文章

  1. SpringBoot的注解注入功能移植到.Net平台(开源)
  2. Java线程池的拒绝策略
  3. servlet request、response的中文乱码问题
  4. Xampp error:Port 80 in use by &quot;Unable to open process&quot; with PID 4
  5. Spring Boot 2.x基础教程:构建RESTful API与单元测试
  6. CF #579 (Div. 3) E.Boxers
  7. java架构之路-(分布式zookeeper)zookeeper集群配置和选举机制详解
  8. Spring框架(三)
  9. 基于hap的文件上传和下载
  10. uC/OS-III 时钟节拍(一)