淺談Spring學(xué)習(xí)之request,session與globalSession作用域
與web容器有關(guān)的作用域,首先要在Web容器里進(jìn)行一些配置。
<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實(shí)例, 且該loginAction bean實(shí)例僅在當(dāng)前HTTP request內(nèi)有效,因此可以根據(jù)需要放心的更改所建實(shí)例的內(nèi)部狀態(tài), 而其他請求中根據(jù)loginAction bean定義創(chuàng)建的實(shí)例,將不會看到這些特定于某個請求的狀態(tài)變化。 當(dāng)處理請求結(jié)束,request作用域的bean實(shí)例將被銷毀。
Session作用域
考慮下面bean定義:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
針對某個HTTP Session,Spring容器會根據(jù)userPreferences bean定義創(chuàng)建一個全新的userPreferences bean實(shí)例, 且該userPreferences bean僅在當(dāng)前HTTP Session內(nèi)有效。 與request作用域一樣,你可以根據(jù)需要放心的更改所創(chuàng)建實(shí)例的內(nèi)部狀態(tài),而別的HTTP Session中根據(jù)userPreferences創(chuàng)建的實(shí)例, 將不會看到這些特定于某個HTTP Session的狀態(tài)變化。 當(dāng)HTTP Session最終被廢棄的時候,在該HTTP Session作用域內(nèi)的bean也會被廢棄掉。
global session作用域
考慮下面bean定義:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>
global session作用域類似于標(biāo)準(zhǔn)的HTTP Session作用域,不過它僅僅在基于portlet的web應(yīng)用中才有意義。Portlet規(guī)范定義了全局Session的概念,它被所有構(gòu)成某個portlet web應(yīng)用的各種不同的portlet所共享。在global session作用域中定義的bean被限定于全局portlet Session的生命周期范圍內(nèi)。
請注意,假如你在編寫一個標(biāo)準(zhǔn)的基于Servlet的web應(yīng)用,并且定義了一個或多個具有g(shù)lobal session作用域的bean,系統(tǒng)會使用標(biāo)準(zhǔn)的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.
<?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é)
以上就是本文關(guān)于Spring學(xué)習(xí)之request,session與globalSession作用域的全部內(nèi)容,希望對大家有所幫助。更多內(nèi)容請參考:Spring spel表達(dá)式使用方法示例、Java之Spring注解配置bean實(shí)例代碼解析、java中javaBean與Bean的深入理解以及本站其他欄目,感謝大家對本站的支持!
- Spring IOC原理補(bǔ)充說明(循環(huán)依賴、Bean作用域等)
- SPRING FRAMEWORK BEAN作用域和生命周期原理解析
- 簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
- Spring實(shí)戰(zhàn)之協(xié)調(diào)作用域不同步的Bean操作示例
- Spring實(shí)戰(zhàn)之Bean的作用域request用法分析
- Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法分析
- 深入了解Spring中Bean的作用域和生命周期
- 淺談Spring中Bean的作用域、生命周期
- spring ioc的簡單實(shí)例及bean的作用域?qū)傩越馕?/a>
- 淺談spring中scope作用域
- JSP 中Spring Bean 的作用域詳解
- 詳解Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式
- 最全總結(jié)SpringBean的作用域管理
相關(guān)文章
Spark操作之a(chǎn)ggregate、aggregateByKey詳解
這篇文章主要介紹了Spark操作之a(chǎn)ggregate、aggregateByKey詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Spring Boot中的 6 種API請求參數(shù)讀取方式示例詳解
使用Spring Boot開發(fā)API的時候,讀取請求參數(shù)是服務(wù)端編碼中最基本的一項(xiàng)操作,Spring Boot中也提供了多種機(jī)制來滿足不同的API設(shè)計要求,這篇文章主要介紹了Spring Boot中的 6 種API請求參數(shù)讀取方式示例詳解,需要的朋友可以參考下2024-05-05InterProcessMutex實(shí)現(xiàn)zookeeper分布式鎖原理
本文主要介紹了InterProcessMutex實(shí)現(xiàn)zookeeper分布式鎖原理,文中根據(jù)實(shí)例編碼詳細(xì)介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03IDEA在一個工作空間中管理多個項(xiàng)目的詳細(xì)步驟
這篇文章主要介紹了IDEA在一個工作空間中管理多個項(xiàng)目的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01