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類型,知足實際應用需求。java

  一、singleton和prototype做用域web

  當一個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); 

上述示例中獲得的時間將是同樣的
修改一下:spring

<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的引用。)設計模式

二、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.一、request
request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP request內有效。session

<bean id="loginAction" class="org.han.action.LoginAction" scope="request"/> 
2.二、session
表示僅在當前會話中有效
<bean id="loginAction" class="org.han.action.LoginAction" scope="session"/> 
2.三、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>  
三、自定義做用域

在spring2.0中做用域是能夠任意擴展的,可是不能覆蓋singleton和prototype,spring的做用域由接口org.springframework.beans.factory.config.Scope來定義,自定義本身的做用域只要實現該接口便可app

相關文章
相關標籤/搜索