---
title: "Spring中bean的作用域"
author: "blueram"
date: "2009-03-21T19:30:46Z"
tags: ["Bean", "IOC", "Spring", "Web", "prototype"]
slug: "spring-bean"
draft: false
lastmod: "2009-03-21T19:30:46Z"
id: 352846
url: "https://blueram.iteye.com/blog/352846"
---

如何使用spring的作用域：

&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton"/&gt;

这里的scope就是用来配置spring bean的作用域，它标识bean的作用域。

在spring2.0之前bean只有2种作用域即：singleton(单例)、non-singleton（也称prototype）, Spring2.0以后，增加了session、request、global session三种专用于Web应用程序上下文的Bean。因此，默认情况下Spring2.0现在有五种类型的Bean。当然，Spring2.0对Bean的类型的设计进行了重构，并设计出灵活的Bean类型支持，理论上可以有无数多种类型的Bean，用户可以根据自己的需要，增加新的Bean类型，满足实际应用需求。

1、singleton作用域

当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例，并且所有对bean的请求，只要id与该bean定义相匹配，则只会返回bean的同一实例。换言之，当把一个bean定义设置为singleton作用域时，Spring IOC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存（singleton cache）中，并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例，这里要注意的是singleton作用域和GOF设计模式中的单例是完全不同的，单例设计模式表示一个ClassLoader中只有一个class存在，而这里的singleton则表示一个容器对应一个bean，也就是说当一个bean被标识为singleton时候，spring的IOC容器中只会存在一个该bean。

配置实例：

&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="singleton"/&gt;

或者

&lt;bean id="role" class="spring.chapter2.maryGame.Role" singleton="true"/&gt;

2、prototype

prototype作用域部署的bean，每一次请求（将其注入到另一个bean中，或者以程序的方式调用容器的`getBean()`方法）都会产生一个新的bean实例，相当与一个new的操作，对于prototype作用域的bean，有一点非常重要，那就是Spring不能对一个prototype bean的整个生命周期负责，容器在初始化、配置、装饰或者是装配完一个prototype实例后，将它交给客户端，随后就对该prototype实例不闻不问了。不管何种作用域，容器都会调用所有对象的初始化生命周期回调方法，而对prototype而言，任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源，都是客户端代码的职责。（让Spring容器释放被singleton作用域bean占用资源的一种可行方式是，通过使用bean的后置处理器，该处理器持有要被清除的bean的引用。）

配置实例：

&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="prototype"/&gt;

或者

&lt;beanid="role" class="spring.chapter2.maryGame.Role" singleton="false"/&gt;

3、request

request表示该针对每一次HTTP请求都会产生一个新的bean，同时该bean仅在当前HTTP request内有效，配置实例：

request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置：

如果你使用的是Servlet 2.4及以上的web容器，那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;web-app&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;listener&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  &lt;/listener&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/web-app&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

，如果是Servlet2.4以前的web容器,那么你要使用一个javax.servlet.Filter的实现：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;web-app&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) ..  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter-name&gt;requestContextFilter&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter-class&gt;org.springframework.web.filter.RequestContextFilter&lt;/filter-class&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/filter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;filter-name&gt;requestContextFilter&lt;/filter-name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/filter-mapping&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/web-app&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

接着既可以配置bean的作用域了：

&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="request"/&gt;

4、session

session作用域表示该针对每一次HTTP请求都会产生一个新的bean，同时该bean仅在当前HTTP session内有效，配置实例：

配置实例：

和request配置实例的前提一样，配置好web启动文件就可以如下配置：

&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="session"/&gt;

5、global session

global session作用域类似于标准的HTTP Session作用域，不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念，它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。如果你在web中使用global session作用域来标识bean，那么web会自动当成session类型来使用。

配置实例：

和request配置实例的前提一样，配置好web启动文件就可以如下配置：

&lt;bean id="role" class="spring.chapter2.maryGame.Role" scope="global session"/&gt;

6、自定义bean装配作用域

在spring2.0中作用域是可以任意扩展的，你可以自定义作用域，甚至你也可以重新定义已有的作用域（但是你不能覆盖singleton和prototype），spring的作用域由接口org.springframework.beans.factory.config.Scope来定义，自定义自己的作用域只要实现该接口即可，下面给个实例：

我们建立一个线程的scope，该scope在表示一个线程中有效，代码如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)publicclass MyScope implements Scope ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      privatefinal ThreadLocal threadScope = new ThreadLocal() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)          protected Object initialValue() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             returnnew HashMap();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     };  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)     public Object get(String name, ObjectFactory objectFactory) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Map scope = (Map) threadScope.get();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Object object = scope.get(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        if(object==null) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           object = objectFactory.getObject();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)           scope.put(name, object);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return object;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)      }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)     public Object remove(String name) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         Map scope = (Map) threadScope.get();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return scope.remove(name);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)      }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      publicvoid registerDestructionCallback(String name, Runnable callback) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)      }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public String getConversationId() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       // TODO Auto-generated method stub  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        returnnull;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)           }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)