SpringRequestContext源码阅读

Posted claindoc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringRequestContext源码阅读相关的知识,希望对你有一定的参考价值。

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 + "]";
    }
}

以上是关于SpringRequestContext源码阅读的主要内容,如果未能解决你的问题,请参考以下文章

我是如何阅读JDK源码的?

阿里P8级别面试官分享出源码阅读技巧附Java源码和大厂真题

源码阅读(28):Java中线程安全的List结构——CopyOnWriteArrayList

JVM GC 分代收集理论的推演(阅读与思考)

JVM GC 分代收集理论的推演(阅读与思考)

素材下载网站代下源码