Spring bean作用域

<bean id="loginAction" class="org.han.action.LoginAction" scope="singleton">  
    <property name="user" ref="user"></property>  
</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和prototype作用域

  当一个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="date" class="java.util.Date" scope="singleton"></bean> 
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml");  
Date d=context.getBean("date",Date.class);  
System.out.println(d);  
Thread.sleep(5000);  
d=context.getBean("date",Date.class);  
System.out.println(d); 

上述示例中得到的时间将是一样的
修改一下:

<bean id="date" class="java.util.Date" scope="prototype"></bean> 

当使用prorotype作为作用域时,Bean会导致每次对该Bean的请求都创建一个Bean实例,所以对有状态的Bean应该使用prorotype作用域,无状态Bean则使用singleton作用域。还有就是Spring不能对一个prototype作用域 bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被singleton作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)

2、Web应用程序上下文的Bean
request、session、global session使用时,首先应该在web.xml中增加如下配置
如果你使用的是Servlet 2.4+的web容器,那么你仅需要在web应用的XML声明文件web.xml中增加下述ContextListener即可:
<web-app>  
    <listener>  
        <listener-class>  
            org.springframework.web.context.request.RequestContextListener  
        </listener-class>  
    </listener>  
</web-app> 

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

<filter>  
    <filter-name>requestContextFilter</filter-name>  
    <filter-class>  
        org.springframework.web.filter.RequestContextFilter  
    </filter-class>  
</filter>  
<filter-mapping>  
    <filter-name>requestContextFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

2.1、request
request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效。

<bean id="loginAction" class="org.han.action.LoginAction" scope="request"/> 
2.2、session
表示仅在当前会话中有效
<bean id="loginAction" class="org.han.action.LoginAction" scope="session"/> 
2.3、global session
global session作用域类似于标准的HTTP Session作用域,不过它仅仅在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念,它被所有构成某个portlet web应用的各种不同的portlet所共享。在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。如果你在web中使用global session作用域来标识bean,那么web会自动当成session类型来使用。
<bean id="loginAction" class="org.han.action.LoginAction" scope="globalSession"/></span>  
3、自定义作用域

在spring2.0中作用域是可以任意扩展的,但是不能覆盖singleton和prototype,spring的作用域由接口org.springframework.beans.factory.config.Scope来定义,自定义自己的作用域只要实现该接口即可

原文地址:https://www.cnblogs.com/jiafuwei/p/6897678.html