代码改变世界

多线程异步操作导致异步线程获取不到主线程的request信息

2020-06-28 17:47  话猫  阅读(2077)  评论(0编辑  收藏  举报

org.springframework.web.context.request.RequestContextHolder
org.springframework.web.context.request.RequestAttributes
org.springframework.web.context.request.ServletRequestAttributes

 

开启新线程之前,添加代码:

// 将RequestAttributes对象设置为子线程共享
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(sra, true);