SpringRequestContext源码阅读

Spring源码关于RequestContext相关信息获取 事件监听器的相关代码实现

1public class RequestContextListener implements ServletRequestListener { 2 private static final String REQUEST_ATTRIBUTES_ATTRIBUTE = RequestContextListener.class 3 .getName() + ".REQUEST_ATTRIBUTES"; 4 5 public void requestInitialized(ServletRequestEvent requestEvent) { 6 if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) { 7 throw new IllegalArgumentException( 8 "Request is not an HttpServletRequest: " 9 + requestEvent.getServletRequest()); 10 } 11 12 HttpServletRequest request = (HttpServletRequest) requestEvent 13 .getServletRequest(); 14 ServletRequestAttributes attributes = new ServletRequestAttributes( 15 request); 16 request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes); 17 LocaleContextHolder.setLocale(request.getLocale()); 18 RequestContextHolder.setRequestAttributes(attributes); 19 } 20 21 public void requestDestroyed(ServletRequestEvent requestEvent) { 22 ServletRequestAttributes attributes = null; 23 Object reqAttr = requestEvent.getServletRequest().getAttribute( 24 REQUEST_ATTRIBUTES_ATTRIBUTE); 25 if (reqAttr instanceof ServletRequestAttributes) { 26 attributes = (ServletRequestAttributes) reqAttr; 27 } 28 RequestAttributes threadAttributes = RequestContextHolder 29 .getRequestAttributes(); 30 if (threadAttributes != null) { 31 LocaleContextHolder.resetLocaleContext(); 32 RequestContextHolder.resetRequestAttributes(); 33 if ((attributes == null) 34 && (threadAttributes instanceof ServletRequestAttributes)) { 35 attributes = (ServletRequestAttributes) threadAttributes; 36 } 37 } 38 if (attributes != null) 39 attributes.requestCompleted(); 40 } 41} 42 43public abstract interface ServletRequestListener extends EventListener { 44 public abstract void requestDestroyed( 45 ServletRequestEvent paramServletRequestEvent); 46 47 public abstract void requestInitialized( 48 ServletRequestEvent paramServletRequestEvent); 49} 50 51public interface EventListener { 52} 53 54 55 56public class ServletRequestEvent extends EventObject { 57 private static final long serialVersionUID = 1L; 58 private final transient ServletRequest request; 59 60 public ServletRequestEvent(ServletContext sc, ServletRequest request) { 61 super(sc); 62 this.request = request; 63 } 64 65 public ServletRequest getServletRequest() { 66 return this.request; 67 } 68 69 public ServletContext getServletContext() { 70 return ((ServletContext) super.getSource()); 71 } 72} 73 74 75 76public class EventObject implements java.io.Serializable { 77 78 private static final long serialVersionUID = 5516075349620653480L; 79 80 /** 81 * The object on which the Event initially occurred. 82 */ 83 protected transient Object source; 84 85 /** 86 * Constructs a prototypical Event. 87 * 88 * @param source The object on which the Event initially occurred. 89 * @exception IllegalArgumentException if source is null. 90 */ 91 public EventObject(Object source) { 92 if (source == null) 93 throw new IllegalArgumentException("null source"); 94 95 this.source = source; 96 } 97 98 /** 99 * The object on which the Event initially occurred. 100 * 101 * @return The object on which the Event initially occurred. 102 */ 103 public Object getSource() { 104 return source; 105 } 106 107 /** 108 * Returns a String representation of this EventObject. 109 * 110 * @return A a String representation of this EventObject. 111 */ 112 public String toString() { 113 return getClass().getName() + "[source=" + source + "]"; 114 } 115}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )