標籤:role 儲存 efi 自動 todo ng2 概念 comm 引用
<bean id="userService" class="com.bjsxt.service.UserService" scope="prototype">
<!--
<property name="userDAO" ref="u" />
-->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
bean的score的取值
1. singleton範圍(scope 預設值)
當一個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="role" class="spring.chapter2.maryGame.Role" scope="singleton"/> 或者<bean id="role" class="spring.chapter2.maryGame.Role" singleton="true"/>
2. prototype
prototype範圍部署的bean,每一次請求(將其注入到另一個bean中,或者以程式的方式調用容器的getBean()方法)都會產生一個新的bean執行個體,相當與一個new的操作,對於prototype範圍的bean,有一點非常重要,那就是Spring不能對一個prototype bean的整個生命週期負責,容器在初始化、配置、裝飾或者是裝配完一個prototype執行個體後,將它交給用戶端,隨後就對該prototype執行個體不聞不問了。不管何種範圍,容器都會調用所有對象的初始化生命週期回調方法,而對prototype而言,任何配置好的析構生命週期回調方法都將不會被調用。 清除prototype範圍的對象並釋放任何prototype bean所持有的昂貴資源,都是用戶端代碼的職責。(讓Spring容器釋放被singleton範圍bean佔用資源的一種可行方式是,通過使用 bean的後置處理器,該處理器持有要被清除的bean的引用。)
配置執行個體:<bean id="role" class="spring.chapter2.maryGame.Role" scope="prototype"/>或者<beanid="role" class="spring.chapter2.maryGame.Role" singleton="false"/>
3. request
request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP request內有效,配置執行個體:request、session、global session使用的時候首先要在初始化web的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的實現:
<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的範圍了:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="request"/>
4. session
session範圍表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP session內有效,配置執行個體:配置執行個體:和request配置執行個體的前提一樣,配置好web開機檔案就可以如下配置:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="session"/>
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開機檔案就可以如下配置:
<bean id="role" class="spring.chapter2.maryGame.Role" scope="global session"/>
6. 自訂bean裝配範圍
在spring2.0中範圍是可以任意擴充的,你可以自訂範圍,甚至你也可以重新定義已有的範圍(但是你不能覆蓋singleton和 prototype),spring的範圍由介面org.springframework.beans.factory.config.Scope來定 義,自訂自己的範圍只要實現該介面即可,下面給個執行個體:我們建立一個線程的scope,該scope在表示一個線程中有效,代碼如下:
publicclass MyScope implements Scope { privatefinal ThreadLocal threadScope = new ThreadLocal() { protected Object initialValue() { returnnew 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; } }
Spring再接觸 Scope範圍