Add to Web. xml
For each HTTP request. The spring container creates a completely new instance of the Loginaction bean based on the loginaction bean definition, and the loginaction bean instance is valid only within the current HTTP request. It is therefore possible to change the internal state of the built instance in confidence. In other requests, instances created from the loginaction bean definition will not see these state changes specific to a request.
When the processing request ends, the bean instance of the request scope is destroyed.
Session Scope
<bean id= "userpreferences" class= "Com.foo.UserPreferences" scope= "session"/>
for an HTTP Session. The spring container creates a completely new instance of the Userpreferences bean based on the userpreferences bean definition, and the userpreferences Bean is valid only within the current HTTP session. As with the request scope, you can change the internal state of the created instance based on the need to be assured, and other HTTP sessions that are created from Userpreferences will not see the state changes that are specific to an HTTP session. When the HTTP session is finally discarded, the beans inside the HTTP session scope are 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, except that it makes sense only in Portlet-based Web applications. The Portlet specification defines the concept of a global session. It is shared by a variety of portlets that make up a portlet Web application.
The bean defined in the global session scope is limited to the lifetime of the world Portlet session.
Note that if you are writing a standard servlet-based Web application and you have defined one or more beans with the global session scope, the system uses the standard HTTP session scope and does not cause any errors
Second, why the need for additional configuration Requestcontextfilter
There may be a doubt that the Web container has been integrated with the spring container through Contextloaderlistener (or Contextloaderservlet). Why is there an additional requestcontextlistener to support the other 3 scopes of the bean, because Contextloaderlistener implements the Servletcontextlistener listener interface, Servletcontextlistener is only responsible for monitoring the startup and shutdown events of the Web container.
The Requestcontextfilter implements the Servletrequestlistener listener interface, which listens for HTTP request events and notifies the listener webserver every request received. The Requestcontextfilter,spring container is more closely combined with the Web container by configuring it.
Three, scope dependence problem
Suppose you inject a web-related scope bean into a singleton or prototype bean, in such a case. Requires 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>
[Java] [web] Use spring to get request and session anywhere