Spring Bean作用域Scope(含SpringCloud中的RefreshScope )

前言

本文主要讨论SpringBean的作用域,我们这里讨论的Bean的作用域,很大程度都是默认只讨论依赖来源为【Spring BeanDefinition】的作用域,因为在我们的业务开发中,我们都是Spring框架的使用者,我们自定义的bean几乎全部都是属于【Spring BeanDefinition】的。后续文章以这个为默认前提。

作用域概览

来源

说明

singleton

默认的spring bean作用域,一个BeanFactory有且仅有一个实例,重要

prototype

原型作用域,每一次的依赖查找和依赖注入都会生成新的bean对象,重要

request

将SpringBean存储在ServletRequest上下文中,不重要

session

将SpringBean存储在HttpSession上下文中,不重要

application

将SpringBean存储在ServletContext上下文中,不重要

由于目前的开发模式基本都是前后端分离,以前我们写JSP的时候需要从后端的response对象中获取部分数据进行展示,现在这些模板技术[JSP,Freemarker,Velocity等等]已经边缘化,这里不会重点讨论后三者作用域。

Singleton作用域

首先我们看一下Spring官方文档的描述:
Only one shared instance of a singleton bean is managed, and all requests for beans with an ID or IDs that match that bean definition result in that one specific bean instance being returned by the Spring container.

To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object. The following image shows how the singleton scope works:

Spring’s concept of a singleton bean differs from the singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as being per-container and per-bean. This means that, if you define one bean for a particular class in a single Spring container, the Spring container creates one and only one instance of the class defined by that bean definition. The singleton scope is the default scope in Spring. To define a bean as a singleton in XML, you can define a bean as shown in the following example:

<bean id="accountService" class="com.something.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.something.DefaultAccountService" scope="singleton"/>

核心点已经高亮出来,对Spring来说,Singleton是指在一个容器中,一般是BeanFactory,只存在一个特定ID的Bean。
singleton作用域也是默认的作用域。

Prototype作用域

依然先看一下Spring官方文档的描述:
The non-singleton prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made. That is, the bean is injected into another bean or you request it through a getBean() method call on the container. As a rule, you should use the prototype scope for all stateful beans and the singleton scope for stateless beans.
我们应该让所有的有状态bean是prototype scope,让所有的无状态bean是singleton scope。

The following diagram illustrates the Spring prototype scope:

 

(A data access object (DAO) is not typically configured as a prototype, because a typical DAO does not hold any conversational state. It was easier for us to reuse the core of the singleton diagram.)
DAO一般不应该被设置为prototype作用域,因为常规的DAO不应该包含任何会话状态。所以应该配置为singleton作用域。它这里只是举例说明。

The following example defines a bean as a prototype in XML:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值