`

ThreadLocal在spring中的使用

 
阅读更多
spring mvc中它的核心类是DispatcherServlet,

在处理请求时,我们看到它如果掉用了doService方法里的doDispath方法。

 protected void doService(HttpServletRequest request, HttpServletResponse response)
    throws Exception
  {
 ......
      doDispatch(request, response);


......
     }


doDispatch 方法中,会去拿RequestContextHolder.getRequestAttributes();

而RequestContextHolder.getRequestAttributes() 方法的实现就是从ThreadLocal中取:
public abstract class RequestContextHolder
{
 private static final ThreadLocal requestAttributesHolder = new ThreadLocal();

  private static final ThreadLocal inheritableRequestAttributesHolder = new InheritableThreadLocal();
  public static RequestAttributes getRequestAttributes()
  {
    RequestAttributes attributes = (RequestAttributes)requestAttributesHolder.get();
    if (attributes == null) {
      attributes = (RequestAttributes)inheritableRequestAttributesHolder.get();
    }
    return attributes;
  }

}


从而保证的request 里面value的线程安全。
分享到:
评论

相关推荐

    Spring定时任务中使用ThreadLocal的坑

    NULL 博文链接:https://bijian1013.iteye.com/blog/2380233

    从ThreadLocal的使用到Spring的事务管理

    NULL 博文链接:https://xxxxxfsadf.iteye.com/blog/518275

    Spring事务处理-ThreadLocal的使用

    NULL 博文链接:https://yizhenn.iteye.com/blog/2293339

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失

    主要介绍了详解Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    运行在tomcat容器中的ThreadLocal容易产生的问题

    运行在tomcat容器中的ThreadLocal容易产生的问题ThreadLocal在tomcat容器中的的生命周期并不等于web request的生命周期,所以(以下讨论的是tomcat容器中使用ThreadLocal),所以ThreadLocal不应保存与请求会影响的...

    Spring.3.x企业应用开发实战(完整版).part2

    12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    高级开发spring面试题和答案.pdf

    Spring中BeanFactory和ApplicationContext的区别 谈谈Spring IOC的理解,原理与实现? bean的生命周期,详细看上面 SpringBoot自动装配的过程的原理: spring的缓存; spring是如何解决的循环依赖; BeanFactory和...

    Spring3.x企业应用开发实战(完整版) part1

    12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...

    Spring基于ThreadLocal的“资源-事务”线程绑定设计的缘起

    题目起的有些拗口了,简单说,这篇文章想要解释Spring为什么会选择使用ThreadLocal将资源和事务绑定到线程上,这背后有着什么样的起因和设计动机,通过分析帮助大家更清晰地认识Spring的线程绑定机制。访问任何带有...

    Java面试 spring知识点 线程池 面试题

    Spring原理 2 Spring ioc 原理 3 Bean生命周期 3 Java反射 4 beanfactory和applicationcontext 5 类装载器ClassLoader 6 Spring aop 原理 6 Aop代理 7 ...ThreadLocal 13 Spring TaskExecutor线程池 16

    aop-log:项目正式命名为aop-log,基于Spring AOP,ThreadLocal实现方法埋点,埋点信息记录和自定义收集

    AopLogAopLog是基于SpringAop和ThreadLocal实现的一个对请求方法埋点记录与处理的日志工具包。设计目的和场景:使用Spring Aop拦截程序,基本上都是同一个小异,不想日后每个项目...在正式环境中,我并不想打印太多无意

    spring boot动态多数据源demo

    在查阅大量多数据源配置资料和文章并结合工作实践的基础上开发了一个简单实用的spring boot动态多数据源demo,该demo主要功能如下: 自定义多数据源类MultiplyDataSource(继承HikariDataSource),通过多线程类...

    spring_batch.zip

    数据源加载在基于DataProperteis的基础上配置了 master slave, 将它们配置成map,map中是多个数据源的主键和数据源属性的映射; 因此, 再通过线程隔离 ThreadLocal 动态数据源上下文来切换数据源; 切换这个操作...

    springboot+springcloud

    springboot+springcloud+eureka+提供者消费者

    java学习整理文档.docx

    java 学习整理文档 Spring框架并...使用ThreadLocal ,在类中定义一个ThreadLocal成员变量,将需要的可变成员变量保存在 ThreadLocal 中(以空间换时间”),为每一个线程都提供了一份变量,因此可以同时访问而互不影响

Global site tag (gtag.js) - Google Analytics