threadLocal的运用

复制代码
 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

        //使用哪一个取决于springmvc在保存request信息时具体用的子类,他们通常无父子单独实现、
        //如果有父子可以继承第一个持有的httpServletRequest
        HttpServletRequest request = ((ServletWebRequest) requestAttributes).getRequest();
        //ServletRequestAttributes它是spring的具体保存的实例实现,通过名字和实现目的 接口是属性目的获取请求servlet
//        ServletRequestAttributes它是最上乘的类其他实现者以继承它保存的request并初始出自己的,并提供自己的get和set
         HttpServletRequest request1 = ((ServletRequestAttributes) requestAttributes).getRequest();
        HttpServletRequest request3 = ((StandardServletAsyncWebRequest) requestAttributes).getRequest();

        String requestURI = request.getRequestURI();
        String method = request.getMethod();
        System.out.println(requestURI+method);

抽象类 RequestContextHolder提供静态方法,获取全局的threadlocal对象,对象中存一个请求属性对象接口,该接口有多个实现类,提供顶层接口以外的附加功能,但在springMVC中把请求保存在当前线程的只会有一个实例实现,其他实例通过继承这个实例同样持有到属性接口实例持有的HttpServletRequest对象,通过 RequestContextHolder类为每一个请求到的线程中都加上一个缓存map,值的接口实例持有请求属性,由于是静态的属性所以所有线程可以使用设置自己的值,获取线程之间存值有交集

复制代码
 private static final ThreadLocal<RequestAttributes> requestAttributesHolder = new NamedThreadLocal("Request attributes");
    private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder = new NamedInheritableThreadLocal("Request context");
相关推荐
c***93771 天前
Spring Security 官网文档学习
java·学习·spring
k***1951 天前
SpringBoot 使用 spring.profiles.active 来区分不同环境配置
spring boot·后端·spring
踏浪无痕1 天前
Maven 依赖冲突的正确处理姿势:别再到处写 exclusion 了
spring boot·spring·maven
过客随尘1 天前
Spring AOP以及事务详解(二)
spring boot·spring
老神在在0011 天前
MyBatis02
后端·spring·java-ee·mvc·mybatis
知其然亦知其所以然1 天前
Java 也能玩高质量 AI 绘图?SpringAI + Azure OpenAI 真香警告!
后端·spring·机器学习
f***6511 天前
spring 跨域CORS Filter
java·后端·spring
Overt0p1 天前
抽奖系统 (1)
java·spring boot·spring·java-ee·java-rabbitmq
王桑.1 天前
WebSocket---一种用于实时传输的网络协议
java·websocket·spring·java-ee
杀死那个蝈坦1 天前
UV 统计(独立访客统计)
java·jvm·spring·kafka·tomcat·maven