Initial web configuration
To support the scoping of beans at the request
, session
,
and global session
levels
(web-scoped beans), some minor initial configuration is required before you define your beans. (This initial setup is not required for the standard scopes, singleton and prototype.)
How you accomplish this initial setup depends on your particular Servlet environment..
If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by the Spring DispatcherServlet
,
or DispatcherPortlet
,
then no special setup is necessary: DispatcherServlet
and DispatcherPortlet
already
expose all relevant state.
If you use a Servlet 2.5 web container, with requests processed outside of Spring’s DispatcherServlet (for example, when using JSF or Struts), you need to register theorg.springframework.web.context.request.RequestContextListener
ServletRequestListener
.
For Servlet 3.0+, this can done programmatically via theWebApplicationInitializer
interface.
Alternatively, or for older containers, add the following declaration to your web application’s web.xml
file:
<web-app> ... <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> ... </web-app>
Alternatively, if there are issues with your listener setup, consider the provided RequestContextFilter
.
The filter mapping depends on the surrounding web application configuration, so you have to change it as appropriate.
<web-app> ... <filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <filter-mapping> <filter-name>requestContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ... </web-app>
DispatcherServlet
, RequestContextListener
and RequestContextFilter
all
do exactly the same thing, namely bind the HTTP request object to the Thread
that
is servicing that request. This makes beans that are request- and session-scoped available further down the call chain.