Bean的作用域


Spring Framework支持五种作用域(其中有三种只能用在基于web的Spring ApplicationContext)。




作用域

描述

singleton

在每个Spring IoC容器中一个bean定义对应一个对象实例。

prototype

一个bean定义对应多个对象实例。

request

在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的SpringApplicationContext情形下有效。

session

在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的SpringApplicationContext情形下有效。

global session

在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的SpringApplicationContext情形下有效。

在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类型,满足实际应用需求。


一、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。



配置实例:



[html] view plain copy print ?


1. <bean id="role"  class="spring.chapter2.maryGame.Role"scope="singleton"/>
2.   
3. 或者  
4.   
5. <bean  id="role" class="spring.chapter2.maryGame.Role"singleton="true"/>



<bean id="role"  class="spring.chapter2.maryGame.Role"scope="singleton"/>

或者

   <bean  id="role" class="spring.chapter2.maryGame.Role"singleton="true"/>



二、prototype



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



配置实例:



[html] view plain copy print ?


1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="prototype"/>
2.   
3. 或者  
4.   
5. <bean  id="role"  class="spring.chapter2.maryGame.Role"  singleton="false"/>


<bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="prototype"/>

或者

<bean  id="role"  class="spring.chapter2.maryGame.Role"  singleton="false"/>

三、request



request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例: request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置:



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



[html] view plain copy print ?


1. <web-app>
2.   ...  
3. <listener>
4. <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
5. </listener>
6.   ...  
7. </web-app>


<web-app>
  ...
  <listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  ...
</web-app>



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



[html] view plain copy print ?


1. <web-app>
2.  ..  
3. <filter>
4. <filter-name>requestContextFilter</filter-name>
5. <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
6. </filter>
7. <filter-mapping>
8. <filter-name>requestContextFilter</filter-name>
9. <url-pattern>/*</url-pattern>
10. </filter-mapping>
11.   ...  
12. </web-app>


<web-app>
 ..
 <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>
  ...
</web-app>



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



[html] view plain copy print ?



    1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="request"/>



    <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="request"/>

    四、session



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



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



    [html] view plain copy print ?



      1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="session"/>



      <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="session"/>

      五、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启动文件就可以如下配置:



      [html] view plain copy print ?


      1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="global session"/>


      <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="global session"/>

      六、自定义bean装配作用域



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



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

      [java] view plain copy print ?


      1. publicclass MyScope implements
      2. private final ThreadLocal threadScope = new
      3. protected
      4. return new
      5.           }   
      6.     };   
      7. public
      8.         Map scope = (Map) threadScope.get();   
      9.         Object object = scope.get(name);   
      10. if(object==null) {   
      11.           object = objectFactory.getObject();   
      12.           scope.put(name, object);   
      13.         }   
      14. return
      15.      }   
      16. public
      17.         Map scope = (Map) threadScope.get();   
      18. return
      19.      }  
      20.      publicvoid registerDestructionCallback(String name, Runnable callback) {   
      21.      }  
      22. public
      23. // TODO Auto-generated method stub
      24.        returnnull;  
      25.     }   
      26.           }


      publicclass MyScope implements Scope { 
           private final ThreadLocal threadScope = new ThreadLocal() {
                protected Object initialValue() {
                  return new HashMap(); 
                } 
          }; 
           public Object get(String name, ObjectFactory objectFactory) { 
              Map scope = (Map) threadScope.get(); 
              Object object = scope.get(name); 
              if(object==null) { 
                object = objectFactory.getObject(); 
                scope.put(name, object); 
              } 
              return object; 
           } 
           public Object remove(String name) { 
              Map scope = (Map) threadScope.get(); 
              return scope.remove(name); 
           }
           publicvoid registerDestructionCallback(String name, Runnable callback) { 
           }
          public String getConversationId() {
             // TODO Auto-generated method stub
             returnnull;
          } 
                }




      Bean的作用域


      Spring Framework支持五种作用域(其中有三种只能用在基于web的Spring ApplicationContext)。




      作用域

      描述

      singleton

      在每个Spring IoC容器中一个bean定义对应一个对象实例。

      prototype

      一个bean定义对应多个对象实例。

      request

      在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的SpringApplicationContext情形下有效。

      session

      在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的SpringApplicationContext情形下有效。

      global session

      在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的SpringApplicationContext情形下有效。

      在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类型,满足实际应用需求。


      一、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。



      配置实例:



      [html] view plain copy print ?



        1. <bean id="role"  class="spring.chapter2.maryGame.Role"scope="singleton"/>
        2.   
        3. 或者  
        4.   
        5. <bean  id="role" class="spring.chapter2.maryGame.Role"singleton="true"/>



        <bean id="role"  class="spring.chapter2.maryGame.Role"scope="singleton"/>
        
        或者
        
           <bean  id="role" class="spring.chapter2.maryGame.Role"singleton="true"/>



        二、prototype



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



        配置实例:



        [html] view plain copy print ?


        1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="prototype"/>
        2.   
        3. 或者  
        4.   
        5. <bean  id="role"  class="spring.chapter2.maryGame.Role"  singleton="false"/>


        <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="prototype"/>
        
        或者
        
        <bean  id="role"  class="spring.chapter2.maryGame.Role"  singleton="false"/>

        三、request



        request表示该针对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效,配置实例: request、session、global session使用的时候首先要在初始化web的web.xml中做如下配置:



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



        [html] view plain copy print ?


        1. <web-app>
        2.   ...  
        3. <listener>
        4. <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        5. </listener>
        6.   ...  
        7. </web-app>


        <web-app>
          ...
          <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
          </listener>
          ...
        </web-app>



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



        [html] view plain copy print ?


        1. <web-app>
        2.  ..  
        3. <filter>
        4. <filter-name>requestContextFilter</filter-name>
        5. <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
        6. </filter>
        7. <filter-mapping>
        8. <filter-name>requestContextFilter</filter-name>
        9. <url-pattern>/*</url-pattern>
        10. </filter-mapping>
        11.   ...  
        12. </web-app>


        <web-app>
         ..
         <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>
          ...
        </web-app>



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



        [html] view plain copy print ?



          1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="request"/>



          <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="request"/>

          四、session



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



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



          [html] view plain copy print ?


          1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="session"/>


          <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="session"/>

          五、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启动文件就可以如下配置:



          [html] view plain copy print ?



            1. <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="global session"/>


            <bean  id="role"  class="spring.chapter2.maryGame.Role"  scope="global session"/>

            六、自定义bean装配作用域



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



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

            [java] view plain copy print ?


            1. publicclass MyScope implements
            2. private final ThreadLocal threadScope = new
            3. protected
            4. return new
            5.           }   
            6.     };   
            7. public
            8.         Map scope = (Map) threadScope.get();   
            9.         Object object = scope.get(name);   
            10. if(object==null) {   
            11.           object = objectFactory.getObject();   
            12.           scope.put(name, object);   
            13.         }   
            14. return
            15.      }   
            16. public
            17.         Map scope = (Map) threadScope.get();   
            18. return
            19.      }  
            20.      publicvoid registerDestructionCallback(String name, Runnable callback) {   
            21.      }  
            22. public
            23. // TODO Auto-generated method stub
            24.        returnnull;  
            25.     }   
            26.           }


            publicclass MyScope implements Scope { 
                 private final ThreadLocal threadScope = new ThreadLocal() {
                      protected Object initialValue() {
                        return new HashMap(); 
                      } 
                }; 
                 public Object get(String name, ObjectFactory objectFactory) { 
                    Map scope = (Map) threadScope.get(); 
                    Object object = scope.get(name); 
                    if(object==null) { 
                      object = objectFactory.getObject(); 
                      scope.put(name, object); 
                    } 
                    return object; 
                 } 
                 public Object remove(String name) { 
                    Map scope = (Map) threadScope.get(); 
                    return scope.remove(name); 
                 }
                 publicvoid registerDestructionCallback(String name, Runnable callback) { 
                 }
                public String getConversationId() {
                   // TODO Auto-generated method stub
                   returnnull;
                } 
                      }