博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringRequestContext源码阅读
阅读量:5098 次
发布时间:2019-06-13

本文共 3764 字,大约阅读时间需要 12 分钟。

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

转载于:https://www.cnblogs.com/claindoc/p/9835237.html

你可能感兴趣的文章
一次动态sql查询订单数据的设计
查看>>
C# 类(10) 抽象类.
查看>>
1.linux ping:unknown host www.***.***
查看>>
http://jingyan.baidu.com/article/dca1fa6fa07000f1a44052f6.html
查看>>
第三方支付架构设计之—帐户体系
查看>>
诸城项目-开发日志
查看>>
fdisk (二) 详解(转)
查看>>
hdu 2768 Cat vs. Dog 最大独立集 巧妙的建图
查看>>
简单将集合的内容转为字符串
查看>>
Python pandas 0.19.1 Intro to Data Structures 数据结构介绍 文档翻译
查看>>
《寿康宝鉴》
查看>>
Mongodb
查看>>
软工个人总结
查看>>
如何将u盘、移动硬盘转化为活动分区--绝招
查看>>
MYSQL 5.7 修改密码、登录问题
查看>>
linux 同步时间 调试core内核
查看>>
PAT Basic 1085
查看>>
springMVC传递一组对象的接受方式
查看>>
收藏一个虚函数表以及虚表指针介绍的文章
查看>>
POJ---2492 A Bug's Life[并查集]
查看>>