Spring中子线程获取RequestAttributes

7 篇文章 0 订阅

一、问题

在子线程中使用下面的方法获取 requestAttributes 为空。

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

二、解决方法

这里提供两种解决方法,方法一适用于某个请求的业务方法;方法二具有通用性,适用于整个项目。

注意:以下两种解决方法,需要在 Servlet 的生命周期内使用,否则会导致 requestAttributes 中获取到的属性为空。

1、方法一

在子线程启动前,加入下面的代码,可以使 requestAttributes 被子线程继承。

// 使子线程也能获取到 requestAttributes
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
RequestContextHolder.setRequestAttributes(requestAttributes, true);

2、方法二

添加一个配置类。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.RequestContextFilter;
import org.springframework.web.servlet.DispatcherServlet;

import javax.annotation.PostConstruct;

/**
 * 子线程继承配置
 */
@Component
public class ThreadContextInheritableConfig {
    @Autowired
    RequestContextFilter requestContextFilter;

    @Autowired
    DispatcherServlet dispatcherServlet;

    @PostConstruct
    public void init() {
        // 设置线程继承属性为true,便于子线程获取到父线程的request,两个都设置为了保险。
        requestContextFilter.setThreadContextInheritable(true);
        dispatcherServlet.setThreadContextInheritable(true);
    }
}

三、原理

Servlet 初始化时,会调用 FrameworkServlet 中的 initContextHolders 方法。

这个方法会设置 requestAttributes ,因为 private boolean threadContextInheritable = false; 所以 requestAttributes 不会被子线程继承。

如果 requestAttributes 需要被子线程继承,就需要设置 threadContextInheritable 为 true 即可。

	private void initContextHolders(HttpServletRequest request,
			@Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) {

		if (localeContext != null) {
			LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
		}
		if (requestAttributes != null) {
			RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
		}
	}
	public static void setRequestAttributes(@Nullable RequestAttributes attributes, boolean inheritable) {
		if (attributes == null) {
			resetRequestAttributes();
		}
		else {
			if (inheritable) {
				inheritableRequestAttributesHolder.set(attributes);
				requestAttributesHolder.remove();
			}
			else {
				requestAttributesHolder.set(attributes);
				inheritableRequestAttributesHolder.remove();
			}
		}
	}

四、参考

springcloud分布式子线程调用feign接口问题

https://blog.csdn.net/u010715243/article/details/109571727

异步线程RequestContextHolder为空问题

https://blog.csdn.net/progammer10086/article/details/109217002

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值