org.springframework.web.filter
public class: RequestContextFilter [javadoc |
source]
java.lang.Object
org.springframework.web.filter.GenericFilterBean
org.springframework.web.filter.OncePerRequestFilter
org.springframework.web.filter.RequestContextFilter
All Implemented Interfaces:
Filter, DisposableBean, BeanNameAware, InitializingBean, ServletContextAware
Servlet 2.3 Filter that exposes the request to the current thread,
through both
org.springframework.context.i18n.LocaleContextHolder and
RequestContextHolder . To be registered as filter in
web.xml.
Alternatively, Spring's org.springframework.web.context.request.RequestContextListener
and Spring's org.springframework.web.servlet.DispatcherServlet also expose
the same request context to the current thread.
This filter is mainly for use with third-party servlets, e.g. the JSF FacesServlet.
Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.
| Methods from org.springframework.web.filter.GenericFilterBean: |
|---|
|
addRequiredProperty, afterPropertiesSet, destroy, getFilterConfig, getFilterName, getServletContext, init, initBeanWrapper, initFilterBean, setBeanName, setServletContext |
| Method from org.springframework.web.filter.RequestContextFilter Detail: |
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
LocaleContextHolder.setLocale(request.getLocale(), this.threadContextInheritable);
RequestContextHolder.setRequestAttributes(attributes, this.threadContextInheritable);
if (logger.isDebugEnabled()) {
logger.debug("Bound request context to thread: " + request);
}
try {
filterChain.doFilter(request, response);
}
finally {
RequestContextHolder.resetRequestAttributes();
LocaleContextHolder.resetLocaleContext();
attributes.requestCompleted();
if (logger.isDebugEnabled()) {
logger.debug("Cleared thread-bound request context: " + request);
}
}
}
|
public void setThreadContextInheritable(boolean threadContextInheritable) {
this.threadContextInheritable = threadContextInheritable;
}
Set whether to expose the LocaleContext and RequestAttributes as inheritable
for child threads (using an java.lang.InheritableThreadLocal ).
Default is "false", to avoid side effects on spawned background threads.
Switch this to "true" to enable inheritance for custom child threads which
are spawned during request processing and only used for this request
(that is, ending after their initial task, without reuse of the thread).
WARNING: Do not use inheritance for child threads if you are
accessing a thread pool which is configured to potentially add new threads
on demand (e.g. a JDK java.util.concurrent.ThreadPoolExecutor ),
since this will expose the inherited context to such a pooled thread. |