已知spring 3+已拥有多种不同的作用域: singleton(默认)、prototype、request、session、global session。(参考: spring中scope作用域(转))

到目前为止,其实还没在项目中实际遇到要修改作用域的情况。

但却知道有大概类似这么一种说法: spring的bean中不允许(或不建议)定义成员变量,不管是public还是private。

但之前在做一个功能的时候确实遇到了想在service定义一个成员变量Map类型的,但有映像spring中默认是单例,结合单例的特性。考虑到可能定义成员变量有问题,所以就重新回来看一下。

(最后也没采用定义成员变量的方式,还是用的参数传递。)

一、测试singleton、prototype的差异

1.1 singleton主要测试代码
@Controller
@Scope("singleton")
public class SingletonController {
@Autowired
private SingletonService singletonService;
private Integer controllerIndex = 1; @RequestMapping("/singleton")
@ResponseBody
public Map<String, Object> singleton(){
Map<String, Object> rs = new HashMap<>();
rs.put("service_index",singletonService.getIndex());
rs.put("controller_index",controllerIndex);
rs.put("controller_hashCode",this.hashCode());
rs.put("service_hashCode",singletonService.hashCode());
rs.put("cache",singletonService.getCache());
return rs;
}
}
@Service
@Scope("singleton")
public class SingletonService {
private Map<String,Object> cache = new HashMap<>();
private Integer index = 1; public Map<String, Object> getCache() {
return cache;
} public Integer getIndex() {
cache.put("index-"+index,index);
return index++;
}
}

结果猜想:

1) 每次请求后controller_index、service_index都在递增。

2) controller_hashCode、service_hashCode每次都保持不变,因为单例只有一个实例。(java中得不到内存地址,变相的hashCode在一定情况下可以表示内存)

3) cache的key/value一直在增多,请求一次多一个。

这些全部都符合单例的特性。

1.2 prototype主要测试代码
@Controller
@Scope("prototype")
public class PrototypeController {
@Autowired
private PrototypeService prototypeService;
private Integer controllerIndex = 1; @RequestMapping("/prototype")
@ResponseBody
public Map<String, Object> singleton(){
Map<String, Object> rs = new HashMap<>();
rs.put("service_index",prototypeService.getIndex());
rs.put("controller_index",controllerIndex);
rs.put("controller_hashCode",this.hashCode());
rs.put("service_hashCode",prototypeService.hashCode());
rs.put("cache",prototypeService.getCache());
return rs;
}
}
@Service
@Scope("prototype")
public class PrototypeService {
private Map<String,Object> cache = new HashMap<>();
private Integer index = 1; public Map<String, Object> getCache() {
return cache;
} public Integer getIndex() {
cache.put("index-"+index,index);
return index++;
}
}

结果猜想:

1) controller_index、service_index始终都是1。

2) controller_hashCode、service_hashCode每次都在改变。

3) cache只有一个key/value,即{"index-1": 1}。

1.3 结论

实际的结果和猜想完全符合,就是简单的单例/多例的区别。

所以如果存在类似的代码:

@Service
@Scope("singleton")
public class SingletonService {
private Map<String,Object> cache = null; public void aMethod() {
cache = new HashMap<>();
// 一些逻辑
cache.put(...);
bMethod();
} public void bMethod() {
Object obj = cache.get(...);
// 一些逻辑
}
}

想现在aMethod()中处理一些逻辑,然后把符合的保存起来供bMethod()使用。并且你简单的测试不会有问题,因为是单线程的测试。

但现在整体来看,如果多个用户同时操作。本来A是put(1,"a"),但是此时B又紧接着put(1,"b")。A先进入bMethod(),那么get(1) = "b",这就是明显的多线程并发问题。

当然可以把Scope改成prototype,但现在的处境是: 1) 没绝对的比较。2) 整个项目中并没有用prototype的先例。

所以最常见的作法是改成方法传参:

@Service
@Scope("singleton")
public class SingletonService { public void aMethod() {
private Map<String,Object> cache = new HashMap<>();
// 一些逻辑
cache.put(...);
bMethod();
} public void bMethod(Map<String,Object> cache) {
Object obj = cache.get(...);
// 一些逻辑
}
}

二、此文的真正目的

简单目的: 在spring默认情况下的bean中定义成员变量带来的风险。

但,其实是记录spring是怎么解决线程安全的。(详见: Spring单例与线程安全小结)

我个人对线程也不是足够了解,去零零碎碎看过,但实际项目中确实还没完全真正的结果过。

但在以前看过过一篇文章(就是上面那篇),写spring是怎么解决线程安全的,写spring利用的不是线程同步机制(synchronized)而是用的ThreadLocal。

这只记录下两者的差异,主要还是上面那篇博客中说的。

synchronized: 时间换空间,以较慢的执行时间来节约空间的被占用。

在同步机制中,通过对象的锁机制保证同一时间只有一个线程访问变量。这时该变量是多个线程共享的,使用同步机制要求程序慎密地分析什么时候对变量进行读写,什么时候需要锁定某个对象,什么时候释放对象锁等繁杂的问题,程序设计和编写难度相对较大。

ThreadLocal: 空间换时间,占用更多的空间来换取较快的执行时间。

在ThreadLocal中,会为每一个线程提供一个独立的变量副本,从而隔离了多个线程对数据的访问冲突。因为每一个线程都拥有自己的变量副本,从而也就没有必要对该变量进行同步了。ThreadLocal提供了线程安全的共享对象,在编写多线程代码时,可以把不安全的变量封装进ThreadLocal。

ps: 最近很长一段时间一直相当烦躁,此文章本来很简单,但写到最后我自己都不知道自己到底想表达什么、想记录什么....感觉更像应付式的给自己一个任务,形式一般的写一篇文章....

【Spring】bean的作用域(@Scope) - singleton、prototype的更多相关文章

  1. Spring bean的作用域和生命周期

    bean的作用域 1.singleton,prototype, web环境下:request,session,gloab session 2.通过scope="" 来进行配置 3. ...

  2. spring之bean的作用域scope的值的详解

    今天研究了一下scope的作用域.默认是单例模式,即 scope="singleton".另外scope还有prototype.request.session.global ses ...

  3. Spring Bean的作用域(转)

    Spring Bean的作用域 .singleton  [单例] eg:<bean id="personService" class="com.yinger.ser ...

  4. spring bean的作用域和自动装配

    1 Bean的作用域 l  singleton单列:整个容器中只有一个对象实例,每次去访问都是访问同一个对象  默认是单列 l  prototype原型: 每次获取bean都产生一个新的对象,比如Ac ...

  5. Spring bean的作用域以及生命周期

    一.request与session的区别 request简介 request范围较小一些,只是一个请求. request对象的生命周期是针对一个客户端(说确切点就是一个浏览器应用程序)的一次请求,当请 ...

  6. spring bean 的作用域

    spring bean 的作用域: 1.单例(singleton):默认是单例模式,也就是说不管给定的bean被注入到其他bean多少次,注入的都是同一个实例. 2.原型(prototype):每次注 ...

  7. Spring——Bean的作用域

    Spring中Bean的作用域有五种,分别是singleton.prototype.request.session.globalSession.其中request.session.globalSess ...

  8. Bean的作用域scope

    Bean的作用域scope 1.singleton 单例,指一个bean容器中只存在一份 2.prototype 每次请求(每次使用)创建新的实例,destroy方式不生效 3.request 每次h ...

  9. spring bean 的作用域之间有什么区别

    spring bean 的作用域之间有什么区别? spring容器中的bean可以分为五个范围.所有范围的名称都是说明的, 1.singleton:这种bean范围是默认的,这种范围确保不管接受到多个 ...

  10. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

随机推荐

  1. Exception | java.security.NoSuchProviderException: no such provider: BC

    背景 今天在用PGP key做JWT签名和验签的时候,转换报了如下错误: org.bouncycastle.openpgp.PGPException: exception on setup: java ...

  2. 对result文件进行数据清洗以及进行可视化

    项目源码地址:https://github.com/gayu121/result(项目里操作的数据都是清洗过后的数据) 测试要求: 1. 数据清洗:按照进行数据清洗,并将清洗后的数据导入hive数据库 ...

  3. C++对于C故有问题的改进

    C++继承了所有的C特性,并且提供了更丰富的语法和特性(OOP支持.模板支持等),并且拥有和C语言同样出色的运行效率.针对C语言的固有问题,C++做出了如下的升级: 所有变量都可以在需要使用时再定义( ...

  4. html作业记录

    <html> <head> <title>Hello World</title> </head> <body> <!-- ...

  5. 1.3.4分析你的第一个Android程序——Android第一行代码(第二版)笔记

    切换项目结构模式 Project模式的项目结构 我们将项目切换成Project模式,这就是真实的目录结构. .gradle和.idea 这两个目下放置的都是Android Studio自动生成的一些文 ...

  6. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  7. [Effective Java 读书笔记] 第二章 创建和销毁对象 第五条

    第五条 避免创建不必要的对象 书中一开始举例: String s = new String("stringette"); // don't do this //应该使用下面,只会创 ...

  8. 准备 Python3 和 Python 虚拟环境

    1.安装依赖包 yum -y install wget gcc epel-release git 2.安装 Python3.6 yum -y install python36 python36-dev ...

  9. docker-enter 安装

    github : https://github.com/sequenceiq/docker-enter [root@localhost ~]# docker run --rm -v /usr/loca ...

  10. centos7安装bind(DNS服务)

    环境介绍 公网IP:149.129.92.239 内网IP:172.17.56.249 系统:CentOS 7.4 一.安装 yum install bind bind-utils -y 二.修改bi ...