标签:set adl mvc 技术 方法 throw 调用 destory ssi
<!-- 此监听器是监听HttpRequest对象,方便ContextHolderUtils程序调用HttpRequest对象 --> <listener> <description>request监听器</description> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
public class RequestContextListener implements ServletRequestListener { private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES"; // 在请求进入的时候,初始化变量放入ThreadLocal<T>中 @Override public void requestInitialized(ServletRequestEvent requestEvent) { //判定当前的requetEvent中获取的ServletRequest()对象是否是HttpServletRequet对象 if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { throw new IllegalArgumentException( "Request is not an HttpServletRequest: " + requestEvent.getServletRequest()); } //强制转型为 HttpServletRequest HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest(); // ServletRequestAttributes 保存了HttpServletRequet、Response、Session等变量 ServletRequestAttributes attributes = new ServletRequestAttributes(request); request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); LocaleContextHolder.setLocale(request.getLocale()); //RequestContextHolder里面有一个ThreadLocal,当前线程共享 RequestContextHolder.setRequestAttributes(attributes); } //在请求被销毁的时候,将在初始化时候的ThreadLocal变量清空。 @Override public void requestDestroyed(ServletRequestEvent requestEvent) { ... } }
public abstract class RequestContextHolder { //ThreadLocal<T>变量用于保存当前线程的共享变量 private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal<RequestAttributes>("Request attributes"); private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder = new NamedInheritableThreadLocal<RequestAttributes>("Request context"); /** * 将线程中的共享变量清除掉,会在RequetContextListner的destory()方法中调用。 */ public static void resetRequestAttributes() { //清空变量 requestAttributesHolder.remove(); inheritableRequestAttributesHolder.remove(); } //过渡方法 public static void setRequestAttributes(RequestAttributes attributes) { setRequestAttributes(attributes, false); } // 核心的方式:将RequetAttrubutes(Request/Response/Session)放入到ThreadLocal<T>中进行共享 public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) { if (attributes == null) { resetRequestAttributes(); } else { if (inheritable) { inheritableRequestAttributesHolder.set(attributes); requestAttributesHolder.remove(); } else { requestAttributesHolder.set(attributes); inheritableRequestAttributesHolder.remove(); } } }
public class ServletRequestAttributes extends AbstractRequestAttributes { private final HttpServletRequest request; private HttpServletResponse response; private volatile HttpSession session; private final Map<String, Object> sessionAttributesToUpdate = new ConcurrentHashMap<String, Object>(1); }
HttpServletRequest request =((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
http://www.cnblogs.com/LiuChunfu/p/7067828.html
由SpringMVC中RequetContextListener说起
标签:set adl mvc 技术 方法 throw 调用 destory ssi
原文地址:http://www.cnblogs.com/softidea/p/7068196.html