深入理解org.springframework.web.context.request.RequestContextHolder

简介: `RequestContextHolder`是Spring提供的一个便捷工具类,用于在非Controller层访问请求上下文信息。通过理解其工作原理和应用场景,可以更好地在Spring应用中管理和使用请求信息,提升代码的可维护性和扩展性。

深入理解 org.springframework.web.context.request.RequestContextHolder

在Spring Web应用中,RequestContextHolder是一个非常有用的工具类,用于在任何地方访问当前请求的上下文信息。它解决了非Controller层或非过滤器层需要访问请求上下文的问题,如在Service层或DAO层。以下是对 RequestContextHolder的详细解析。

1. RequestContextHolder简介

RequestContextHolder是Spring提供的一个持有器(holder)类,用于存储和获取当前线程的请求相关信息。它通过ThreadLocal机制,将当前请求的上下文信息与线程绑定,从而使得在应用的任何地方都能访问到这些信息。

2. RequestContextHolder的主要方法

2.1 getRequestAttributes

public static RequestAttributes getRequestAttributes() {
    return requestAttributesHolder.get();
}
​

返回当前线程绑定的 RequestAttributes,如果没有绑定则返回 null

2.2 setRequestAttributes

public static void setRequestAttributes(@Nullable RequestAttributes attributes) {
    requestAttributesHolder.set(attributes);
}
​

RequestAttributes绑定到当前线程。

2.3 resetRequestAttributes

public static void resetRequestAttributes() {
    requestAttributesHolder.remove();
}
​

解除当前线程的 RequestAttributes绑定。

2.4 currentRequestAttributes

public static RequestAttributes currentRequestAttributes() throws IllegalStateException {
    RequestAttributes attributes = getRequestAttributes();
    if (attributes == null) {
        throw new IllegalStateException("No thread-bound request found: " +
            "Are you referring to request attributes outside of an actual web request, " +
            "or processing a request outside of the originally receiving thread? " +
            "If you are actually operating within a web request and still receive this message, " +
            "your code is probably running outside of DispatcherServlet: " +
            "In this case, use RequestContextListener or RequestContextFilter to expose the current request.");
    }
    return attributes;
}
​

获取当前线程的 RequestAttributes,如果没有绑定则抛出 IllegalStateException

3. RequestContextHolder的应用场景

3.1 在Service层访问请求信息

在Service层或其他非Controller的地方,需要访问当前请求的信息,可以使用 RequestContextHolder

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class MyService {

    public void logRequestDetails() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            String sessionId = request.getSession().getId();
            System.out.println("Current Session ID: " + sessionId);
        }
    }
}
​

3.2 在自定义拦截器中使用

自定义拦截器中可以通过 RequestContextHolder获取请求信息来做日志记录或其他处理。

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest currentRequest = attributes.getRequest();
            String requestURI = currentRequest.getRequestURI();
            System.out.println("Incoming request: " + requestURI);
        }
        return true;
    }
}
​

4. 注意事项

  1. ThreadLocal的使用RequestContextHolder使用 ThreadLocal存储请求上下文,因此在并发环境下,每个线程都有独立的请求上下文。
  2. 请求生命周期:确保在请求生命周期内使用 RequestContextHolder,否则可能会抛出 IllegalStateException
  3. 集成RequestContextListener或RequestContextFilter:在非Spring管理的线程中,需要使用 RequestContextListenerRequestContextFilter来确保请求上下文的可用性。

分析说明表

方法 说明 示例代码
getRequestAttributes 获取当前线程绑定的 RequestAttributes RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
setRequestAttributes 绑定 RequestAttributes到当前线程 RequestContextHolder.setRequestAttributes(attributes);
resetRequestAttributes 解除当前线程的 RequestAttributes绑定 RequestContextHolder.resetRequestAttributes();
currentRequestAttributes 获取当前线程的 RequestAttributes,无则抛异常 RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();

结论

RequestContextHolder是Spring提供的一个便捷工具类,用于在非Controller层访问请求上下文信息。通过理解其工作原理和应用场景,可以更好地在Spring应用中管理和使用请求信息,提升代码的可维护性和扩展性。

目录
相关文章
|
5月前
|
监控 前端开发 Java
深入理解 org.springframework.web.servlet.AsyncHandlerInterceptor
深入理解 org.springframework.web.servlet.AsyncHandlerInterceptor
|
6月前
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
SpringCloud启动Consider defining a bean of type ‘org.springframework.web.client.RestTemplate‘ in your
123 1
解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchExceptio
解决:org.springframework.web.method.annotation.MethodArgumentTypeMismatchExceptio
465 0
|
Java Spring
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
required a bean of type ‘org.springframework.web.client.RestTemplate‘ that could not be found.
No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessageConverters‘ avail
No qualifying bean of type ‘org.springframework.boot.autoconfigure.http.HttpMessageConverters‘ avail
158 1
|
前端开发 Java 应用服务中间件
Error configuring application listener of class org.springframework.web.context.ContextLoader
Error configuring application listener of class org.springframework.web.context.ContextLoader
Error configuring application listener of class org.springframework.web.context.ContextLoader
|
Java 应用服务中间件 Maven
Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
109 0
|
算法 Java API
org.springframework.web.client.HttpClientErrorException: 429 Too Many Requests
org.springframework.web.client.HttpClientErrorException: 429 Too Many Requests
441 0
|
缓存 Java Maven
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
132 0
严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
|
JSON Dubbo Java
Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:
Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody:
260 0
Failed to meta-introspect annotation interface org.springframework.web.bind.annotation.RequestBody: