<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="singleton"/>
scope是用来配置spring bean的作用域。spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称prototype), Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的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。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="singleton"/>
或者
<bean id="test" class="com.makocn.spring.bean.scope.Test" singleton="true"/>
2、prototype
prototype作用域的bean,在每次请求都会产生新的bean实例,相当与new的操作。Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或装配完一个prototype实例后,将它交给客户端后就对该prototype实例不闻不问了。对于其他作用域的BEAN,容器都会调用对象的初始化生命周期回调方法,对prototype析构生命周期回调方法不会被调用。清除prototype作用域对象并释放prototype bean持有的资源,由客户端代码负责。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="prototype"/>
或者
<bean id="test" class="com.makocn.spring.bean.scope.Test" singleton="false"/>
3、request
request表示针对每次HTTP请求都产生新的bean,同时该bean在request内有效。
说明: 使用request、session、global session时先要在web.xml中进行配置:
(1)Servlet 2.4及以上的web容器,增加ContextListener即可:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
(1)ervlet2.4以前的web容器,那么使用javax.servlet.Filter的实现:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="request"/>
4、session
session作用域表示针对每次HTTP请求都会产生一个新的bean,同时该bean在session内有效。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="session"/>
5、global session
global session作用域类似于标准Session作用域,但仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。如果在web中使用global session作用域来标识bean,那么web会自动当成session类型来使用。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="global session"/>
6、自定义作用域
可自定义作用域,甚至重新定义已有的作用域(但不能覆盖singleton和prototype),spring的作用域由接口org.springframework.beans.factory.config.Scope定义,自定义作用域只要实现该接口即可。
举例:建立一个线程的scope,该scope在表示一个线程中有效,代码如下:
public class ThreadScope {
private final ThreadLocal threadScope = new ThreadLocal() {
protected Object initialValue() {
return new HashMap();
}
};
scope是用来配置spring bean的作用域。spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称prototype), Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的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。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="singleton"/>
或者
<bean id="test" class="com.makocn.spring.bean.scope.Test" singleton="true"/>
2、prototype
prototype作用域的bean,在每次请求都会产生新的bean实例,相当与new的操作。Spring不能对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或装配完一个prototype实例后,将它交给客户端后就对该prototype实例不闻不问了。对于其他作用域的BEAN,容器都会调用对象的初始化生命周期回调方法,对prototype析构生命周期回调方法不会被调用。清除prototype作用域对象并释放prototype bean持有的资源,由客户端代码负责。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="prototype"/>
或者
<bean id="test" class="com.makocn.spring.bean.scope.Test" singleton="false"/>
3、request
request表示针对每次HTTP请求都产生新的bean,同时该bean在request内有效。
说明: 使用request、session、global session时先要在web.xml中进行配置:
(1)Servlet 2.4及以上的web容器,增加ContextListener即可:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
(1)ervlet2.4以前的web容器,那么使用javax.servlet.Filter的实现:
<web-app>
...
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
...
</web-app>
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="request"/>
4、session
session作用域表示针对每次HTTP请求都会产生一个新的bean,同时该bean在session内有效。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="session"/>
5、global session
global session作用域类似于标准Session作用域,但仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。如果在web中使用global session作用域来标识bean,那么web会自动当成session类型来使用。
配置实例:
<bean id="test" class="com.makocn.spring.bean.scope.Test" scope="global session"/>
6、自定义作用域
可自定义作用域,甚至重新定义已有的作用域(但不能覆盖singleton和prototype),spring的作用域由接口org.springframework.beans.factory.config.Scope定义,自定义作用域只要实现该接口即可。
举例:建立一个线程的scope,该scope在表示一个线程中有效,代码如下:
public class ThreadScope {
}