在2.0之前只有两种singleton和prototype(网上说的,没去验证),后面增加了session、request、global session三种专门用于web应用程序上下文的Bean

  Singleton

  这是spring的bean的默认的作用域-单例。但是此单例又与设计模式中的单例模式不一样,设计模式的单例在设计模式的文章中再介绍。

  singleton不要简单的理解为全局只有一个,真正的意义是在一个上下文(ApplicationContext)中只有一个。如果在一个项目中有多个ApplicationContext,那么获取的Bean对象就不只一个了

  在同一个容器或上下文中,所有对单例bean的请求,只要id与该bean定义相匹配,都会返回同一个实例。简单说就是bean定义为singleton时,ioc容器只会创建该bean的唯一实例,然后存储在单例缓存(singleton cache)中。

  singleton的bean是无状态的,只有一份,所以无论哪个线程获取的都是同一个同样的实例,具有线程无关性。Spring使用ThreadLocal解决线程安全问题,这就要求在使用singleton的bean时不能存在属性的改变,如果存在属性改变,就需要慎重使用,采用同步来处理多线程问题,或者考虑使用prototype作用域。

  基于上面,我们常用的Controller、Service、Repository、Configuration这些bean基本上都是项目启动就已经初始化完毕,每个bean的属性都已经确定,在运行过程中也不会更改,这样就很适合单例模式。这里再说一下实体类(用@Entity注解修饰的)这个可不是一个bean,试想一下,每次使用实体的时候是不是都是DomainA a = new DomainA();,应该没有人这么用@Autowired private DomainA domianA;

  使用scope的方法如下:

  @Scope(singleton)

  @Component

  public class MainService {...}

  Prototype

  相对应singleton,prototype就属于多例模式,每次请求都会创建一个新的实例,相当于new操作。

  对于prototype类型的bean,spring只负责创建初始化,不会对整个生命周期负责,随后的所有操作都交给客户端来处理

  现在问一个问题,如何声明一个prototype的bean并使用呢?(先不要急着往下看,想一下,就以在ScopeTestController里面注入prototype的PrototypeService来说明)

  ~

  ~

  ~

  ~

  思考中...

  ~

  ~

  ~

  ~

  可能很多人(包括我)开始会以为像以下这种写法就可以

  public interface PrototypeService {

  }

  @Service

  @Scope(prototype)

  public class PrototypeServiceImpl implements PrototypeService {

  }

  @RestController

  public class ScopeTestController {

  @Autowired

  PrototypeService prototypeService;

  @GetMapping(/)

  public void testPrototype() {

  System.out.println(prototypeService);

  }

  }

  启动项目,两次次请求,查看控制台输出

  com.ukirin.idle.web.service.impl.PrototypeServiceImpl@74738f89

  com.ukirin.idle.web.service.impl.PrototypeServiceImpl@74738f89

  ?一样?什么情况?

  其实仔细想一下也就明白了错在哪,Controller是一个单例,在启动时就已经把Service注入了,所以不可能改变,当然现在你可以这么改,将Controller同样改为prototype的。那么恭喜你回答正确,但是,这不是我们的目的,我们的目的是要看在单例中如何使用。

  方法1:从ApplicationContext里面获取

  将Controller里面的代码做以下改动

  @RestController

  public class ScopeTestController {

  // @Autowired

  // PrototypeService prototypeService;

  @Autowired

  WebApplicationContext applicationContext;

  @GetMapping(/)

  public void testPrototype() {

  // System.out.println(prototypeService);

  System.out.println(applicationContext.getBean(PrototypeService.class));

  }

  }

  这样每次都从上下文中请求一个实例,容器对每个请求都实例化一个新的Service,没有问题

  方法2:方法1的变种,采用工厂模式来生成Service

  代码就不写了...

  方法3:使用代理

  很简单,在@Scope里面加上代理模式即可

  @Service

  @Scope(value = prototype, proxyMode = ScopedProxyMode.INTERFACES)

  public class PrototypeServiceImpl implements PrototypeService {

  }

  @RestController

  public class ScopeTestController {

  @Autowired

  PrototypeService prototypeService;

  // @Autowired

  // WebApplicationContext applicationContext;

  @GetMapping(/)

  public void testPrototype() {

  System.out.println(prototypeService);

  // System.out.println(applicationContext.getBean(PrototypeService.class));

  }

  }

  这样,就可以每次获取不同的Service了,这个的原理就是,在初始化Controller的时候并不是讲一个Service的实体注入,而是注入一个代理,当真正调用Service的时候,代理会对其进行依赖解析,并调用真正的实体bean

  额外需要注意的一点是,如果注入的不是接口的实现,而是一个类,那么需要将proxyMode = ScopedProxyMode.INTERFACES改为proxyMode = ScopedProxyMode.TARGET_CLASS

  request

  该作用域将 bean 的定义限制为 HTTP 请求。只在 web-aware Spring ApplicationContext 的上下文中有效

  上述的实验结果是每次请求都会输出不一样的结果,在这里可能会与prototype产生困惑,做以下的实验可以解决你的困惑

  public interface PrototypeService {

  }

  @Service

  @Scope(value = request, proxyMode = ScopedProxyMode.INTERFACES)

  public class PrototypeServiceImpl implements PrototypeService {

  }

  public interface MiddleService {

  void test();

  }

  @Service

  public class MiddleServiceImpl implements MiddleService {

  @Autowired

  PrototypeService prototypeService;

  @Override

  public void test() {

  System.out.println(middle : + prototypeService);

  }

  }

  @RestController

  public class ScopeTestController {

  @Autowired

  MiddleService middleService;

  @Autowired

  PrototypeService prototypeService;

  // @Autowired

  // WebApplicationContext applicationContext;

  @GetMapping(/)

  public void testPrototype() {

  System.out.println(controller : + prototypeService);

  // System.out.println(applicationContext.getBean(PrototypeService.class));

  middleService.test();

  }

  }

  输出结果为:

  controller : com.ukirin.idle.web.service.impl.PrototypeServiceImpl@6b90371c

  middle : com.ukirin.idle.web.service.impl.PrototypeServiceImpl@6b90371c

  然后将作用域改为prototype再测试一下

  输出结果为:

  controller : com.ukirin.idle.web.service.impl.PrototypeServiceImpl@49b2c498

  middle : com.ukirin.idle.web.service.impl.PrototypeServiceImpl@ccb8c47

  结果显而易见

  session

  该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。

  上述的实验结果是一个会话内输出结果是一样的

  global-session

  该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。

Spring scope解惑的更多相关文章

  1. Spring Scope:Web项目中如何安全使用有状态的Bean对象?

    Web系统是最常见的Java应用系统之一,现在流行的Web项目多使用ssm或ssh框架,使用spring进行bean的管理,这为我们编写web项目带来了很多方便,通常,我们的controler层使用注 ...

  2. Spring scope

    scope用来声明IOC容器中的对象应该处的限定场景或者说该对象的存活空间,即在IOC容器在 对象进入相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁 ...

  3. spring scope="prototype", scope="session"

    转自: http://www.cnblogs.com/JemBai/archive/2010/11/10/1873954.html struts+spring action应配置为scope=&quo ...

  4. Spring——scope详解(转载)

    摘自<spring 解密> scope用来声明IOC容器中的对象应该处的限定场景或者说该对象的存活空间,即在IOC容器在 对象进入相应的scope之前,生成并装配这些对象,在该对象不再处于 ...

  5. spring scope 作用域

    转自:http://www.cnblogs.com/qq78292959/p/3716827.html 今天研究了一下scope的作用域.默认是单例模式,即scope="singleton& ...

  6. Spring scope注解

    Spring注解中@scope默认为单例模式(singleton) 设置写法@scope("") 1.singleton单例模式 全局有且仅有一个实例 2.prototype原型模 ...

  7. Spring scope 配置

    Scope 描述的是 Spring 容器如何新建Bean的实例,Spring的Scope有以下几种,通过@Scope来注解实现: 1. Singleton: 一个Spring容器中只有一个Bean的实 ...

  8. spring scope 属性的取值

    Spring 容器是通过单例模式创建 Bean 对象的,也就是说,默认情况下,通过调用 ac.getBean("mybean")方法获得的对象都是同一个 mybean 对象 使用单 ...

  9. spring scope prototype与singleton区别

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

随机推荐

  1. The Thinking of AutomaticTest(有关自动化测试的思考)

    考虑因素: 容易维护 简洁易懂 代码重用性好 系统的稳定性强 UI自动化:   数据的获取:装载的数据文件类型.数据的形式.数据的解析方法定义. 1.利用Junit单元测试组织用例,明确输入数据.预期 ...

  2. LCS/LIS/LCIS 模板总结

    /************************* LCS/LIS/LCIs模板总结: *************************/ /*************************** ...

  3. Share and NTFS Permission

    NTFS Permissions Share Permissions Share and NTFS Permission Similarities 共享权限和NTFS权限的相似性 Modifying ...

  4. OracleUNDO

    UNDO作用 数据的回滚 一致性读 表的闪回(事务,查询的闪回....) 失败会话的恢复 数据的回滚 SQL> rollback; 回滚的过程就是从回滚段里拿到刚刚执行的这条语句产生的回滚,然后 ...

  5. Duilib 入门级教程 推荐

    http://www.cnblogs.com/Alberl/category/520438.html 作者写的不错,图文并茂,适合刚入门.

  6. python装饰器的学习笔记

    此博文是我对装饰器的一些理解,如果有错误欢迎及时留言,我会第一时间向大家学习. 一.什么是装饰器 1.从字面意义来看: 是用来给函数装饰打扮的函数 2.理论上可以理解为: (1).不改变函数的运行方式 ...

  7. 2015-03-22——js常用的Array方法

    Array array.concat(item...);  //产生一个新数组如果item,是一个数组,那么它的每个元素会被分别添加(浅复制,只解析一层).示例:var a = [1, 3, 4];v ...

  8. Using Swift with Cocoa and Objective-C--在同个project中使用Swift和在同个project中

    http://www.cocoachina.com/newbie/basic/2014/0605/8688.html watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5u ...

  9. Centos7管理selinux及使用firewalld管理防火墙

    CentOS 7.0默认使用的是firewall作为防火墙 1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status ...

  10. 60. Permutation Sequence(求全排列的第k个排列)

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...