Spring源码关于RequestContext相关信息获取 事件监听器的相关代码实现
public class RequestContextListener implements ServletRequestListener { private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class .getName() + ".REQUEST_ATTRIBUTES"; public void requestInitialized(ServletRequestEvent requestEvent) { if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { throw new IllegalArgumentException( "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); } HttpServletRequest request = (HttpServletRequest) requestEvent .getServletRequest(); ServletRequestAttributes attributes = new ServletRequestAttributes( request); request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); LocaleContextHolder.setLocale(request.getLocale()); RequestContextHolder.setRequestAttributes(attributes); } public void requestDestroyed(ServletRequestEvent requestEvent) { ServletRequestAttributes attributes = null; Object reqAttr = requestEvent.getServletRequest().getAttribute( REQUEST_ATTRIBUTES_ATTRIBUTE); if (reqAttr instanceof ServletRequestAttributes) { attributes = (ServletRequestAttributes) reqAttr; } RequestAttributes threadAttributes = RequestContextHolder .getRequestAttributes(); if (threadAttributes != null) { LocaleContextHolder.resetLocaleContext(); RequestContextHolder.resetRequestAttributes(); if ((attributes == null) && (threadAttributes instanceof ServletRequestAttributes)) { attributes = (ServletRequestAttributes) threadAttributes; } } if (attributes != null) attributes.requestCompleted(); }}public abstract interface ServletRequestListener extends EventListener { public abstract void requestDestroyed( ServletRequestEvent paramServletRequestEvent); public abstract void requestInitialized( ServletRequestEvent paramServletRequestEvent);}public interface EventListener {}public class ServletRequestEvent extends EventObject { private static final long serialVersionUID = 1L; private final transient ServletRequest request; public ServletRequestEvent(ServletContext sc, ServletRequest request) { super(sc); this.request = request; } public ServletRequest getServletRequest() { return this.request; } public ServletContext getServletContext() { return ((ServletContext) super.getSource()); }}public class EventObject implements java.io.Serializable { private static final long serialVersionUID = 5516075349620653480L; /** * The object on which the Event initially occurred. */ protected transient Object source; /** * Constructs a prototypical Event. * * @param source The object on which the Event initially occurred. * @exception IllegalArgumentException if source is null. */ public EventObject(Object source) { if (source == null) throw new IllegalArgumentException("null source"); this.source = source; } /** * The object on which the Event initially occurred. * * @return The object on which the Event initially occurred. */ public Object getSource() { return source; } /** * Returns a String representation of this EventObject. * * @return A a String representation of this EventObject. */ public String toString() { return getClass().getName() + "[source=" + source + "]"; }}