Use Spring to get Request and Session anytime, anywhere

Use Spring to get Request and Session anytime, anywhere

1. Preparation:

 

add in web.xml  

 

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

 

2. How to use: 
1. Method 1: Realize through code 

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


2. Method 2: Implement through annotations: 

 

 

@Autowired
private  HttpServletRequest request;
3. Background knowledge about RequestContextListener:
Binds the HTTP request object to the thread serving the request based on LocalThread. This enables request and session scoped beans to be accessed later in the call chain.  

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


each HTTP request, the Spring container will create a new LoginAction bean instance according to the loginAction bean definition, and the loginAction The bean instance is only valid within the current HTTP request, so you can safely change the internal state of the created instance as needed, while the instances created according to the loginAction bean definition in other requests will not see these request-specific state changes . When processing the request ends, the request-scoped bean instance will be destroyed.  

Session scope  
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>  
For an HTTP Session, the Spring container will create a new userPreferences bean instance based on the userPreferences bean definition, and the userPreferences bean is only valid within the current HTTP Session. As with the request scope, you can safely change the internal state of the created instance as needed, while instances created according to userPreferences in other HTTP Sessions will not see these HTTP Session-specific state changes. When the HTTP Session is finally discarded, the beans in the scope of the HTTP Session are also discarded.  

global session scope  

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

global session scope is similar to the standard HTTP Session scope, but only in portlet-based web applications make sense. The Portlet specification defines the concept of a global Session, which is shared by all the various portlets that make up a portlet web application. Beans defined in the global session scope are limited to the lifetime scope of the global portlet Session.  

Please note that if you are writing a standard servlet-based web application and define one or more beans with global session scope, the system will use the standard HTTP Session scope and will not cause any errors  
. 2. Why Requires additional configuration RequestContextFilter  
There may be a question, the Web container has been integrated with the Spring container through the ContextLoaderListener (or ContextLoaderServlet), why is there an additional RequestContextListener to support the other three scopes of the Bean, the reason is that the ContextLoaderListener implements the ServletContextListener listener interface, and the ServletContextListener It is only responsible for monitoring the startup and shutdown events of the web container. RequestContextFilter implements the ServletRequestListener listener interface, which listens for HTTP request events and notifies the listener for each request received by the Web server. By configuring the RequestContextFilter, the Spring container is more closely integrated with the Web container.  
3. Scope dependency problem  

If you inject a Web-related scoped bean into a singleton or prototype bean, in this case, Spring AOP is required

<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>  

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326994764&siteId=291194637