ThreadLocal和InheritableThreadLocal深入探究(二)在Spring中的应用

ThreadLocal和InheritableThreadLocal深入探究(二)在Spring中的应用

SpringRequestContextHolderThreadLocalInheritableThreadLocal的实现:

org.springframework.web.context.request.RequestContextHolder

该类暴露了web请求属性约束的支持,即请求中属性值将会被子类线程继承,当开关inheritable打开的时候

Holder class to expose the web request in the form of a thread-bound
{@link RequestAttributes} object. The request will be inherited
by any child threads spawned by the current thread if the
{@code inheritable} flag is set to {@code true}.

源码分析

public abstract class RequestContextHolder  {
    // 定义NamedThreadLocal
    private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
        new NamedThreadLocal<RequestAttributes>("Request attributes");
    // 定义NamedInheritableThreadLocal
    private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
        new NamedInheritableThreadLocal<RequestAttributes>("Request context");

    // attributes表示开关
    public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
        if (attributes == null) {
            resetRequestAttributes();
        }
        else {
            // 开关打开采用NamedInheritableThreadLocal
            if (inheritable) {
                inheritableRequestAttributesHolder.set(attributes);
                requestAttributesHolder.remove();
            }
            // 开关关闭采用NamedThreadLocal
            else {
                requestAttributesHolder.set(attributes);
                inheritableRequestAttributesHolder.remove();
            }
        }
    }
public abstract class FrameworkServlet extends HttpServletBean implements ApplicationContextAware {
    // 设置 threadContextInheritable 默认值
    private boolean threadContextInheritable = false;
    //web servlet 的开关默认值为 false
    public void setThreadContextInheritable(boolean threadContextInheritable) {
        this.threadContextInheritable = threadContextInheritable;
    }
    ...
        private void initContextHolders(
        HttpServletRequest request, LocaleContext localeContext, RequestAttributes requestAttributes) {

        // setLocaleContext
        if (localeContext != null) {
            LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
        }
        // setRequestAttributes 设置ThreadLocal和InheritableThreadLocal的开关this.threadContextInheritable
        if (requestAttributes != null) {
            // 实现对RequestContextHolder初始化的支持
            RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Bound request context to thread: " + request);
        }
    }
    ...
}

总结

经过对Spring源码的分析,我们得出在Spring Web Mvc中对于ThreadLocal父子类继承关系的动态设置,以及调用、实现。

相关文章推荐

ThreadLocal深入理解与内存泄漏分析
https://blog.csdn.net/shang_xs/article/details/87874477
ThreadLocal和InheritableThreadLocal深入探究(一)源码分析
https://blog.csdn.net/shang_xs/article/details/87889079
ThreadLocal和InheritableThreadLocal深入探究(三)在Spring Cloud Netflix中的应用
https://blog.csdn.net/shang_xs/article/details/87889285

猜你喜欢

转载自blog.csdn.net/shang_xs/article/details/87889219