RequestContextHolder详解

发布时间:2023-05-19

一、概述

RequestContextHolder是Spring框架中作为HTTP请求上下文绑定的持有者,可以方便地随时获取当前请求的HttpServletRequest、HttpServletResponse等信息,这对于Web开发来说是不可或缺的。 在Web应用程序中,一个请求可能会经过多个线程,每个线程都会处理请求的某一个环节。RequestContextHolder可以确保在整个请求处理过程中,我们都能够访问到该请求的相关信息。

二、RequestContextHolder分类

Spring框架提供了两种不同类型的RequestContextHolder:

1、RequestContextHolder使用ThreadLocal保存请求信息

public class RequestContextHolder {
   private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
           new NamedThreadLocal("Request attributes");
   public static void resetRequestAttributes() {
       requestAttributesHolder.remove();
   }
   public static void setRequestAttributes(RequestAttributes attributes) {
       Assert.notNull(attributes, "Only non-null request attributes are permitted");
       requestAttributesHolder.set(attributes);
   }
   public static RequestAttributes getRequestAttributes() {
       RequestAttributes attributes = requestAttributesHolder.get();
       if (attributes == null) {
           attributes = new NativeWebRequestAttributes(new MockHttpServletRequest());
           requestAttributesHolder.set(attributes);
       }
       return attributes;
   }
   public static void setRequestAttributes(ServletRequest request, ServletResponse response) {
       setRequestAttributes(new ServletWebRequest(request, response));
   }
   public static void setRequestAttributes(ServletRequestAttributes attributes) {
       Assert.notNull(attributes, "ServletRequestAttributes must not be null");
       requestAttributesHolder.set(attributes);
   }
   public static ServletRequestAttributes getRequestAttributes() {
       RequestAttributes attributes = getRequestAttributes();
       Assert.state(attributes instanceof ServletRequestAttributes,
               "Request attribute does not support request access");
       return (ServletRequestAttributes) attributes;
   }
}

上述代码中,使用ThreadLocal保存请求的信息,如从HttpServletRequest中获取的请求信息,这样可以保证每个线程都能够访问到已绑定请求的信息,线程之间不会相互影响。

2、RequestContextHolder使用InheritableThreadLocal保存请求信息

public class RequestContextHolder {
   private static final InheritableThreadLocal<RequestAttributes> requestAttributesHolder =
           new NamedInheritableThreadLocal("Request attributes");
   public static void resetRequestAttributes() {
       requestAttributesHolder.remove();
   }
   public static void setRequestAttributes(RequestAttributes attributes) {
       Assert.notNull(attributes, "Only non-null request attributes are permitted");
       requestAttributesHolder.set(attributes);
   }
   public static RequestAttributes getRequestAttributes() {
       RequestAttributes attributes = requestAttributesHolder.get();
       if (attributes == null) {
           attributes = new NativeWebRequestAttributes(new MockHttpServletRequest());
           requestAttributesHolder.set(attributes);
       }
       return attributes;
   }
   public static void setRequestAttributes(ServletRequest request, ServletResponse response) {
       setRequestAttributes(new ServletWebRequest(request, response));
   }
   public static void setRequestAttributes(ServletRequestAttributes attributes) {
       Assert.notNull(attributes, "ServletRequestAttributes must not be null");
       requestAttributesHolder.set(attributes);
   }
   public static ServletRequestAttributes getRequestAttributes() {
       RequestAttributes attributes = getRequestAttributes();
       Assert.state(attributes instanceof ServletRequestAttributes,
               "Request attribute does not support request access");
       return (ServletRequestAttributes) attributes;
   }
}

与第一种方式不同的是,该代码中使用InheritableThreadLocal保存请求的信息,InheritableThreadLocal是ThreadLocal类的子类,它所保存的值可以被子线程继承,在子线程中可以访问到保存的信息,由于InheritableThreadLocal是可继承的,所以是跨线程的,内存消耗很大,开发时需慎重考虑。

三、RequestContextHolder的用法

1、获取当前请求的HttpServletRequest

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

2、获取当前请求的HttpServletResponse

HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();

3、获取当前请求的RequestAttributes

RequestAttributes attributes = RequestContextHolder.getRequestAttributes();

4、通过RequestContextHolder为当前请求创建的子线程传递请求上下文

在多个线程的情况下,如果想把同一个请求传递给同一个线程来进行处理,可以先在原请求线程中获取当前请求上下文,并传递到另外一个线程中,代码示例如下: 原请求线程中:

RequestAttributes attributes = RequestContextHolder.getRequestAttributes();

新线程中:

RequestContextHolder.setRequestAttributes(attributes);

5、在Controller层使用RequestContextHolder的示例:

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("/getRequest")
    public String getRequest(HttpServletRequest request) {
        HttpSession session = request.getSession();
        request.setAttribute("name", "test");
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getAttribute("name").toString();
    }
}

四、小结

RequestContextHolder是Spring框架中作为HTTP请求上下文绑定的持有者,可以方便地随时获取当前请求的HttpServletRequest、HttpServletResponse等信息,能够确保在整个请求处理过程中,我们都能够访问到该请求的相关信息,对Web开发来说非常重要。