SpringMVC中RequetContextListener
来自:https://www.cnblogs.com/softidea/p/7068196.html
零、引言
一、web.xml配置
<!-- 此监听器是监听HttpRequest对象,方便ContextHolderUtils程序调用HttpRequest对象 -->
<listener>
<description>request监听器</description>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
二、三个重要类解读
2.1 RequetContextListener
public class RequestContextListener implements ServletRequestListener {
private static final String REQUEST_ATTRIBUTES_ATTRIBUTE =
RequestContextListener.class.getName() + ".REQUEST_ATTRIBUTES";
// 在请求进入的时候,初始化变量放入ThreadLocal<T>中 ,创建request对象
@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) {
...
}
}
2.2 RequetContextHolder
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();
}
}
}
2.3 ServletRequestAttributes
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();
四、总结
SpringMVC中RequetContextListener的更多相关文章
- 由SpringMVC中RequetContextListener说起
零.引言 RequetContextListener从名字结尾Listener来看就知道属于监听器. 所谓监听器就是监听某种动作,在其开始(初始化)和结束(销毁)的时候进行某些操作. 由此可以猜测:该 ...
- SpringMvc中的数据校验
SpringMvc中的数据校验 Hibernate校验框架中提供了很多注解的校验,如下: 注解 运行时检查 @AssertFalse 被注解的元素必须为false @AssertTrue 被注解的元素 ...
- 【Spring】SpringMVC中浅析Date类型数据的传递
在控制器中加入如下代码: @InitBinder public void initBinder(ServletRequestDataBinder bin){ SimpleDateFormat sdf ...
- 详解SpringMVC中GET请求
GET请求概述 GET请求,请求的数据会附加在URL之后,以?分割URL和传输数据,多个参数用&连接.URL的编码格式采用的是ASCII编码,而不是uniclde,所有的非ASCII字符都要编 ...
- SpringMVC中使用Interceptor拦截器
SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.比如通过它来进行权限验证,或者是来判断用户是否登陆,或者是像12306 那 ...
- 如何在springMVC 中对REST服务使用mockmvc 做测试
如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试 spring 集成测试中对mock 的集成实在是太棒了!但 ...
- springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序 http://www.360doc.com/content/14/03 ...
- SpringMVC中使用Cron表达式的定时器
SpringMVC中使用Cron表达式的定时器 cron(定时策略)简要说明 顺序: 秒 分 时 日 月 星期 年份 (7个参数,空格隔开各个参数,年份非必须参数) 通配符: , 如果分钟位置为* 1 ...
- SpringMVC中使用Json传数据
在web项目中使用Json进行数据的传输是非常常见且有用的,在这里介绍下在SpringMVC中使用Json传数据的一种方法,在我的使用中,主要包括下面四个部分(我个人喜好使用maven这类型工具进行项 ...
随机推荐
- Entity Framework Tutorial Basics(17):DBSet Class
DBSet Class DBSet class represents an entity set that is used for create, read, update, and delete o ...
- 学会使用postman工具模拟请求-----待补充
登录: backstop 密码:backstop的密码 记得加上header,在swagger中有content-type. 请求,则是api下对应的请求. get请求直接加入链接即可. post请求 ...
- 安装postman工具模拟请求
扩展工具->: 注意,在创建快捷方式后,第一次打开这个工具,会让你注册信息,忽略它.关闭,重新打开postman工具,然后如下所示:
- 浅谈assert()函数的用法
#include<stdio.h> #include<assert.h> char * Strcpy(char *dst,const char *src) { assert(d ...
- android listview addHeaderView和addFooterView的注意事项
1. item内如果有button等控件时,在监听listview的onitemclick事件时,焦点会被item内的button. imagebutton等控件抢走,从而导致在listview设置了 ...
- Java50道经典习题-程序46 字符串连接
题目:编写一个两个字符串连接的程序 import java.util.Scanner; public class Prog46 { public static void main(String[] a ...
- easyui textbox 设置只读不可编辑状态
在使用easyul的时候,发现输入框内容及不容易获取与设置,用jQuery的方式大部分失效.依稀记得好像是因为easyul会在原页面的基础上,生成了一些新的独有样式,并且暂时覆盖掉使用了easyul的 ...
- for循环 break
for (int i = 1; i <= 8;i++) {} for (int i=10; i>=1;i--) i的起始值是10 着次递减 for(){}嵌套放便控制行列的长短 break ...
- No module named 'numpy.core._multiarray_umath'
问题:基于anaconda prompt 安装好TensorFlow框架以后,引入的时候(import tensorflow as tf)报如下图片的错误: 回答:网上好多人说是需要升级numpy,我 ...
- 【bzoj4800】: [Ceoi2015]Ice Hockey World Championship dfs
[bzoj4800]: [Ceoi2015]Ice Hockey World Championship N<=40所以如果直接dfs背包会TLE 考虑Meet-in-the-middle 如果把 ...