SpringMVC之RequestContextHolder分析

最近遇到的問題是在service獲取request和response,正常來說在service層是沒有request的,然而直接從controlller傳過來的話解決方法太粗暴,後來發現了SpringMVC提供的RequestContextHolder遂去分析一番,並藉此對SpringMVC的結構深入瞭解一下,後面會再發文章詳細分析源碼


1.RequestContextHolder的使用

RequestContextHolder顧名思義,持有上下文的Request容器.使用是很簡單的,具體使用如下:

//兩個方法在沒有使用JSF的項目中是沒有區別的
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//                                            RequestContextHolder.getRequestAttributes();
//從session裏面獲取對應的值
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);

HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();

看到這一般都會想到幾個問題:

  1. request和response怎麼和當前請求掛鉤?
  2. request和response等是什麼時候設置進去的?

2.解決疑問

2.1 request和response怎麼和當前請求掛鉤?

首先分析RequestContextHolder這個類,裏面有兩個ThreadLocal保存當前線程下的request,關於ThreadLocal可以參考我的另一篇博文[Java學習記錄--ThreadLocal使用案例]

//得到存儲進去的request
private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
new NamedThreadLocal<RequestAttributes>("Request attributes");
//可被子線程繼承的request
private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
new NamedInheritableThreadLocal<RequestAttributes>("Request context");

再看`getRequestAttributes()`方法,相當於直接獲取ThreadLocal裏面的值,這樣就保證了每一次獲取到的Request是該請求的request.

    public static RequestAttributes getRequestAttributes() {
        RequestAttributes attributes = requestAttributesHolder.get();
        if (attributes == null) {
            attributes = inheritableRequestAttributesHolder.get();
        }
        return attributes;
    }

2.2request和response等是什麼時候設置進去的?

找這個的話需要對springMVC結構的`DispatcherServlet`的結構有一定了解才能準確的定位該去哪裏找相關代碼.

在IDEA中會顯示如下的繼承關係.

左邊1這裏是Servlet的接口和實現類.

右邊2這裏是使得SpringMVC具有Spring的一些環境變量和Spring容器.類似的XXXAware接口就是對該類提供Spring感知,簡單來說就是如果想使用Spring的XXXX就要實現XXXAware,spring會把需要的東西傳送過來.

那麼剩下要分析的的就是三個類,簡單看下源碼

1. HttpServletBean 進行初始化工作

2. FrameworkServlet 初始化 WebApplicationContext,並提供service方法預處理請

3. DispatcherServlet 具體分發處理.

那麼就可以在FrameworkServlet查看到該類重寫了service(),doGet(),doPost()...等方法,這些實現裏面都有一個預處理方法`processRequest(request, response);`,所以定位到了我們要找的位置

查看`processRequest(request, response);`的實現,具體可以分爲三步:

  1. 獲取上一個請求的參數
  2. 重新建立新的參數
  3. 設置到XXContextHolder
  4. 父類的service()處理請求
  5. 恢復request
  6. 發佈事件

protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
//獲取上一個請求保存的LocaleContext
    LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
//建立新的LocaleContext
    LocaleContext localeContext = buildLocaleContext(request);
//獲取上一個請求保存的RequestAttributes
    RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
//建立新的RequestAttributes
    ServletRequestAttributes requestAttributes = buildRequestAttributes(request, 
response, previousAttributes);
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), 
new RequestBindingInterceptor());
//具體設置的方法
    initContextHolders(request, localeContext, requestAttributes);
try {
        doService(request, response);
    }
catch (ServletException ex) {
failureCause = ex;
throw ex;
    }
catch (IOException ex) {
   failureCause = ex;
   throw ex;
    }
catch (Throwable ex) {
   failureCause = ex;
   throw new NestedServletException("Request processing failed", ex);
    }
finally {
//恢復
        resetContextHolders(request, previousLocaleContext, previousAttributes);
if (requestAttributes != null) {
requestAttributes.requestCompleted();
        }
if (logger.isDebugEnabled()) {
if (failureCause != null) {
this.logger.debug("Could not complete request", failureCause);
            }
else {
if (asyncManager.isConcurrentHandlingStarted()) {
                    logger.debug("Leaving response open for concurrent processing");
                }
else {
this.logger.debug("Successfully completed request");
                }
            }
        }
//發佈事件
        publishRequestHandledEvent(request, response, startTime, failureCause);
    }
}

再看initContextHolders(request, localeContext, requestAttributes)方法,把新的RequestAttributes設置進LocalThread,實際上保存的類型爲ServletRequestAttributes,這也是爲什麼在使用的時候可以把RequestAttributes強轉爲ServletRequestAttributes.


private void initContextHolders(HttpServletRequest request, 
                                LocaleContext localeContext, 
                                RequestAttributes requestAttributes) {
if (localeContext != null) {
        LocaleContextHolder.setLocaleContext(localeContext, 
this.threadContextInheritable);
    }
if (requestAttributes != null) {
        RequestContextHolder.setRequestAttributes(requestAttributes, 
this.threadContextInheritable);
    }
if (logger.isTraceEnabled()) {
        logger.trace("Bound request context to thread: " + request);
    }
}

因此RequestContextHolder裏面最終保存的爲ServletRequestAttributes,這個類相比`RequestAttributes`方法是多了很多.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章