與web容器有關的作用域,首先要在Web容器里進行一些配置。
1
2
3
4
5
6
7
8
9
|
< web-app > ... < listener > < listener-class > org.springframework.web.context.request.RequestContextListener </ listener-class > </ listener > ... </ web-app > |
Request作用域
考慮下面bean定義:
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>
針對每次HTTP請求,Spring容器會根據(jù)loginAction bean定義創(chuàng)建一個全新的LoginAction bean實例, 且該loginAction bean實例僅在當前HTTP request內(nèi)有效,因此可以根據(jù)需要放心的更改所建實例的內(nèi)部狀態(tài), 而其他請求中根據(jù)loginAction bean定義創(chuàng)建的實例,將不會看到這些特定于某個請求的狀態(tài)變化。 當處理請求結(jié)束,request作用域的bean實例將被銷毀。
Session作用域
考慮下面bean定義:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
針對某個HTTP Session,Spring容器會根據(jù)userPreferences bean定義創(chuàng)建一個全新的userPreferences bean實例, 且該userPreferences bean僅在當前HTTP Session內(nèi)有效。 與request作用域一樣,你可以根據(jù)需要放心的更改所創(chuàng)建實例的內(nèi)部狀態(tài),而別的HTTP Session中根據(jù)userPreferences創(chuàng)建的實例, 將不會看到這些特定于某個HTTP Session的狀態(tài)變化。 當HTTP Session最終被廢棄的時候,在該HTTP Session作用域內(nèi)的bean也會被廢棄掉。
global session作用域
考慮下面bean定義:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global session作用域類似于標準的HTTP Session作用域,不過它僅僅在基于portlet的web應用中才有意義。Portlet規(guī)范定義了全局Session的概念,它被所有構(gòu)成某個portlet web應用的各種不同的portlet所共享。在global session作用域中定義的bean被限定于全局portlet Session的生命周期范圍內(nèi)。
請注意,假如你在編寫一個標準的基于Servlet的web應用,并且定義了一個或多個具有global session作用域的bean,系統(tǒng)會使用標準的HTTP Session作用域,并且不會引起任何錯誤。
作用域依賴問題
If you want to inject (for example) an HTTP request scoped bean into another bean of a longer-lived scope, you may choose to inject an AOP proxy in place of the scoped bean. That is, you need to inject a proxy object that exposes the same public interface as the scoped object but that can also retrieve the real target object from the relevant scope (such as an HTTP request) and delegate method calls onto the real object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- an HTTP Session-scoped bean exposed as a proxy --> < bean id = "userPreferences" class = "com.foo.UserPreferences" scope = "session" > <!-- instructs the container to proxy the surrounding bean --> < aop:scoped-proxy /> </ bean > <!-- a singleton-scoped bean injected with a proxy to the above bean --> < bean id = "userService" class = "com.foo.SimpleUserService" > <!-- a reference to the proxied userPreferences bean --> < property name = "userPreferences" ref = "userPreferences" /> </ bean > </ beans > |
總結(jié)
以上就是本文關于Spring學習之request,session與globalSession作用域的全部內(nèi)容,希望對大家有所幫助。感謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/qq_33665647/article/details/53212226