scope作用域:
1.prototype
2.request
3.session
4.singleton
5.global session
1.prototype(多例)
prototype作用域部署的bean,每一次請求(將其注入到另一個bean中,或者以程序的方式調用容器的 getBean()方法)都會產生一個新的bean實例,相當與一個new的操作。
配置實例:
<bean id="happy" class="cn.hq.entlty.Student" scope="prototype"/>
效果實現:
2.request
request表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP request內有效。
配置實例(初始化web.xml中配置):
<bean id="happy" class="cn.hq.entlty.Student" scope="request"/>
web.xml
<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>
3.session
session作用域表示該針對每一次HTTP請求都會產生一個新的bean,同時該bean僅在當前HTTP session內有效。
配置實例:前提和request相似在web.xml里進行配置
<bean id="happy" class="cn.hq.entlty.Student" scope="request"/>
4.singleton
配置實例:
單例,在Spring IoC容器中僅存在一個Bean實例(默認的scope),spring默認在spring容器中只有一個Bean實例對象。
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.xml
駿馬是跑出來的,強兵是打出來的。
---真理