IT忍者神龟之Bean scope

3.5 Bean scopes

When you create a bean definition, you create a recipe(配方) for creating actual(真实的,实际的) instances of the class defined by that bean definition. The idea(想法) that a bean definition is a recipe
is important(重要的), because it means that, as with a class, you can create many object instances from a single recipe.

You can control not only the various(不同的) dependencies and configuration values that are to be plugged into(插入到) an object that is created from a particular bean definition, but also the scope of
the objects created from a particular bean definition. This approach(做法) is powerful(强大) and flexible(灵活) in that you can choose the scope of the objects you create through configuration instead of having to bake in the
scope of an object at the Java class level. Beans can be defined to be deployed(部署) in one of a number of scopes: out of the box, the Spring Framework supports five scopes, three of which are available only if you use a web-aware ApplicationContext.

The following scopes are supported out of the box. You can also create a
custom scope.

Table 3.3. Bean scopes

Scope Description

singleton


(Default) Scopes a single bean definition to a single object instance per(每个) Spring IoC container.


prototype


Scopes a single bean definition to any number of object instances.


request


Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.


session


Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.


global
session


Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

Thread-scoped beans

As of Spring 3.0, a thread scope is available, but is not registered by default. For more information, see the documentation forSimpleThreadScope.
For instructions on how to register this or any other custom scope, see Section 3.5.5.2,
“Using a custom scope”.

3.5.1 The singleton scope

Only one shared instance of a singleton bean is managed, and all requests for beans with an id or ids matching 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.

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 per container
and per bean
. This means that if you define one bean for a particular class in a single Spring container, then 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 would write, for example:

3.5.2 The prototype scope

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(一般来说), use the prototype scope for all stateful(有状态) beans and the singleton scope for stateless(无状态)
beans.

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 just easier for
this author to reuse the core of the singleton diagram.

3.5.4 Request, session, and global session scopes

The requestsession, and global session scopes
are only available if you use a web-aware Spring ApplicationContext implementation (such asXmlWebApplicationContext). If you use these scopes with regular
Spring IoC containers such as the ClassPathXmlApplicationContext, you get an IllegalStateException complaining about an unknown bean scope.

时间: 2024-10-08 15:03:31

IT忍者神龟之Bean scope的相关文章

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

默认@Bean是单例的,但可以使用@Scope注解来覆盖此如下: @Configuration public class MyConfiguration { @Bean @Scope("prototype") public MovieCatalog movieCatalog(){ //... } } Bean的作用域包括singleton.prototype.request.session.global session 例子:

每日一大片之忍者神龟变种时代

忍者神龟变种时代BD 主演:梅根·福克斯  阿伦·瑞奇森  诺尔·费舍  皮特·普劳泽克  杰瑞米·霍华德  威尔·阿奈特   类型:动作片 导演:乔纳森·理贝斯曼   地区:美国 年份:2014 语言: 介 绍:繁华大都会美国纽约,邪恶领袖施莱德(水源士郎 饰)率领的大脚帮在城市内为非作歹,甚嚣尘上.安保公司老板萨克斯(威廉·菲德内尔 饰)推出最新安保系统,发誓维护城市的和平.第6频道女记者爱普尔·奥尼尔(梅根·福克斯 饰)偶然拍到大脚帮被四名神秘侠客挫败的画面,在第二次遭遇非常事件时,她惊讶

Spring Bean Scope

In Spring, bean scope is used to decide which type of bean instance should be return from Spring container back to the caller. 5 types of bean scopes supported : singleton – Return a single bean instance per Spring IoC container prototype – Return a

IT忍者神龟之mysql远程连接:ERROR 1130 (HY000): Host '*.*.*.*' is not allowed to connect to this MySQL server解决

安装完MySQL后,远程连接数据库的时候,出现 ERROR 1130 (HY000): Host '192.168.0.1' is not allowed to connect to this MySQL server提示信息,不能远程连接数据库.考虑可能是因为系统数据库mysql中user表中的host是localhost的原因,于是,我尝试把这个值改为自己服务器的ip,果然就好用了,不过用 mysql -u root -p命令就连不上数据库了,需要用mysql -h 服务器ip -u roo

Spring基础 快速入门spring(11) bean scope注解方式

在前面我们已经学习过spring中的bean scope, 温故而知新,这次我们将使用注解的方式来验证singleton和prototype的区别. bean scope 在spring中,bean的lifecyle大体如下所示 种类 详细 singleton (Default) Scopes a single bean definition to a single object instance per Spring IoC container. prototype Scopes a sing

Spring Bean Scope 有状态的Bean 无状态的Bean

http://blog.csdn.net/anyoneking/article/details/5182164 在Spring的Bean配置中,存在这样两种情况: [xhtml] view plaincopy <bean id="testManager" class="com.sw.TestManagerImpl" scope="singleton" /> <bean id="testManager" cla

bean scope scoped-proxy

1. singleton 配置中的bean定义可以看作是一个模板,容器会根据这个模板来构造对象.但是要根据这个模板构造多少对象实例,又该让这些构造完的对象实例存活多久,则由容器根据bean定义的scope语意来决定.标记为拥有singleton scope的对象定义,在Spring的IoC容器中只存在一个实例,所有对该对象的引用将共享这个实例.该实例从容器启动,并因为第一次被请求而初始化之后,将一直存活到容器退出,也就是说,它与IoC容器"几乎"拥有相同的"寿命".

IT忍者神龟之Struts2-Json-Plugin 的使用(翻译自官方文档)

在 Struts2 中要使用 Ajax 获得 Json 数据我认为目前还是 struts2-json-plugin 了.当然你你可以用手工用像 XStream.Google Gson.Jackson 这样的工具手工把 Java 对象转换成 Json 字符串再写往 Response 去,要写的代码自然多不了,还得留心字符集与 content type.而 struts2-json-plugin 毫无疑问是与 Struts2 最亲近了,只需你配置一些属性就能得到你想的结果. 本想分几篇逐步介绍如何使

IT忍者神龟之Spring+MyBatis多数据源配置实现

最近用到了MyBatis配置多数据源,原以为简单配置下就行了,实际操作后发现还是要费些事的,这里记录下,以作备忘 不多废话,直接上代码,后面会有简单的实现介绍 jdbc和log4j的配置 #定义输出格式 ConversionPattern=%d %-5p [%t] %c - %m%n log4j.rootLogger=DEBUG,Console log4j.logger.com.cnblogs.lzrabbit=DEBUG log4j.logger.org.springframework=ERR