[Java][web]利用Spring随时随地获得Request和Session

利用Spring随时随地获得Request和Session

一、准备工作:

在web.xml中加入  

<listener>    
        <listener-class>    
            org.springframework.web.context.request.RequestContextListener
        </listener-class>    
</listener>

 


二、用法:
1、方法一:通过代码实现

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();



2、方法二:通过注解实现:

@Autowired
private  HttpServletRequest request;



三、关于RequestContextListener的背景知识:
基于LocalThread将HTTP request对象绑定到为该请求提供服务的线程上。这使得具有request和session作用域的bean可以在后面的调用链中被訪问到。 

Request作用域 
<bean id="loginAction" class="com.foo.LoginAction" scope="request"/> 


针对每次HTTP请求。Spring容器会依据loginAction bean定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此能够依据须要放心的更改所建实例的内部状态。而其它请求中依据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。

当处理请求结束,request作用域的bean实例将被销毁。 

Session作用域 
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/> 
针对某个HTTP Session。Spring容器会依据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,你能够依据须要放心的更改所创建实例的内部状态,而别的HTTP Session中依据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session终于被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。

 

global session作用域 

<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/> 

global session作用域类似于标准的HTTP Session作用域,只是它只在基于portlet的web应用中才有意义。Portlet规范定义了全局Session的概念。它被全部构成某个portlet web应用的各种不同的portlet所共享。

在global session作用域中定义的bean被限定于全局portlet Session的生命周期范围内。 

请注意,假如你在编写一个标准的基于Servlet的web应用,而且定义了一个或多个具有global session作用域的bean,系统会使用标准的HTTP Session作用域,而且不会引起不论什么错误 
二、为什么须要额外的配置RequestContextFilter 
或许会有一个疑问,已经通过ContextLoaderListener(或ContextLoaderServlet)将Web容器与Spring容器整合。为什么这里还要用额外的RequestContextListener以支持Bean的另外3个作用域,原因是ContextLoaderListener实现ServletContextListener监听器接口,而ServletContextListener仅仅负责监听Web容器的启动和关闭的事件。

RequestContextFilter实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,Webserver接收的每次请求都会通知该监听器。通过配置RequestContextFilter,Spring容器与Web容器结合的更加密切。

 
三、作用域依赖问题 

假设将Web相关作用域的Bean注入到singleton或prototype的Bean中,这样的情况下。须要Spring AOP

<bean name="car" class="com.demo.Car" scope="request">  

    <aop:scoped-proxy/>  

</bean>  

<bean id="boss" class="com.demo.Boss" >  

   <properrty name="car" ref="car" />  

</bean>  


原文地址:https://www.cnblogs.com/llguanli/p/7372894.html