最詳細(xì)的SpringMVC執(zhí)行流程

SpringMVC執(zhí)行流程圖

1.png

在前端控制器中最最重要的方法是 doDispatch,在這個方法中 ,起到委派模式中委派者的角色,負(fù)責(zé)把 任務(wù)分發(fā)給各個角色做處理

分發(fā)的主要任務(wù):

  1. 獲取處理器映射器

  2. 根據(jù)處理器映射器獲取處理器適配器

  3. 根據(jù)處理器適配器獲取視圖ModelAndView

  4. 使用視圖解析解解析視圖

  5. 渲染視圖

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //把request對象賦值給 processedRequest
        HttpServletRequest processedRequest = request;
        //定義 HandlerExecutionChain 如果是訪問controller方法的話,封裝方法對象(方法對象中封裝了controller對象),  HandlerExecutionChain還將封裝所有的攔截器
        HandlerExecutionChain mappedHandler = null;
        boolean multipartRequestParsed = false;

        WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

        try {
            //將會通過 ha.handle(processedRequest, response, mappedHandler.getHandler()); 獲取 ModelAndView
            ModelAndView mv = null;
            Exception dispatchException = null;

            try {
                // 1.嘗試將當(dāng)前請求轉(zhuǎn)換為MultipartHttpServletRequest
                processedRequest = checkMultipart(request);
                multipartRequestParsed = (processedRequest != request);

                
        // 2.查找當(dāng)前請求對應(yīng)的handler,包括Handler(控制器也就是controller本身)本身和Handler攔截器

        //當(dāng)遍歷到requestMappingHandlerMapping時  在requestMappingHandlerMapping中其實存儲了  所有攔截器的對象
                /**
                 * 這個方法一路千辛萬苦,一路封裝,滿載而歸
                 *  1.首先是調(diào)用AbstractHandlerMapping的getHandler方法,然后調(diào)用  AbstractHandlerMethodMapping的getHandlerInternal方法
                 *  然后重新創(chuàng)建創(chuàng)建對象 new HandlerMethod(this, handler) 把controller對象(從工廠中獲取) 賦值 給方法對象HandlerMethod
                 *  所以第一步就是: 把controller對象賦值給方法對象
                 *
                 *  2.然后調(diào)用AbstractHandlerMapping.getHandlerExecutionChain 轉(zhuǎn)換為 HandlerExecutionChain 對象
                 *  遍歷攔截器集合 把所有的攔截器對象賦值給HandlerExecutionChain對象
                 *  所以第二步就是: 把方法對象轉(zhuǎn)換為HandlerExecutionChain對象并把所有的攔截器賦值到其中
                 *
                 */
                //根據(jù)請求request對象,調(diào)用處理器映射器尋找處理器,其實就是 HandlerExecutionChain 對象
                mappedHandler = getHandler(processedRequest);
                /**
                 * 此時的 mappedHandler 即為  HandlerExecutionChain 對象
                 * HandlerExecutionChain 對象中 封裝了瀏覽器訪問的方法對應(yīng)的方法對象,方法對象中封裝了對象的controller對象,HandlerExecutionChain封裝了所有的攔截器
                 */


                // 未能找到對應(yīng)的handler,拋出NoHandlerFoundException異常并返回404
                if (mappedHandler == null) {
                    noHandlerFound(processedRequest, response);
                    return;
                }

                // Determine handler adapter for the current request.
                // 3.查找當(dāng)前請求對應(yīng)的HandlerAdapter
                //把方法對象傳進去,獲取到一個適配器
                HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

                // Process last-modified header, if supported by the handler.
                // 4.處理last-modified請求頭,如果當(dāng)前請求支持的話
                //獲取方法的請求方法
                String method = request.getMethod();

                boolean isGet = "GET".equals(method);
                if (isGet || "HEAD".equals(method)) {
                    long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
                    if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
                        return;
                    }
                }

                // 5.應(yīng)用前置攔截器
                // 如果有攔截器返回false,則表明該攔截器已經(jīng)處理了返回結(jié)果,直接返回;  
                //注意:   此時的 processedRequest 其實就是request 對象
                //就是在判斷定義所有的攔截器 的前置方法,返回的到底是true,還是false
                //如果有一個前置返回的是false,那么停止執(zhí)行下面的代碼,  只有所有的攔截器的前置方法返回的true才可以
                if (!mappedHandler.applyPreHandle(processedRequest, response)) {
                    return;
                }


                // Actually invoke the handler.
                // 6.調(diào)用HandlerAdapter的handler方法,真正開始處理Controller

                //在以上的所有步驟中,   ModelAndView都還沒有返回
                //這個方法嘗試獲取  ModelAndView 對象   把request對象 ,response對象 和方法對象傳進去

                //進入到RequestMappingHandlerAdaper適配器的handleInternal

                //準(zhǔn)備獲取ModelAndView對象 同時在方法里面執(zhí)行了controller方法的內(nèi)容
                mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
                //此時ModelAndView對象的view值為跳轉(zhuǎn)的路徑

                // 7.如果當(dāng)前請求是并發(fā)處理,直接返回
                if (asyncManager.isConcurrentHandlingStarted()) {
                    return;
                }

                // 8. 如果當(dāng)前返回值中不包含視圖名的話,為返回值設(shè)定默認(rèn)視圖名,
                //意思如果你沒有設(shè)置跳轉(zhuǎn)路徑的話,這個方法默認(rèn)給你加跳轉(zhuǎn)路徑
                applyDefaultViewName(processedRequest, mv);

                // 9.應(yīng)用已注冊攔截器的后置方法。
                //倒著遍歷所有的攔截器 先注冊的后執(zhí)行
            mappedHandler.applyPostHandle(processedRequest, response, mv);
        }
            catch (Exception ex) {
                dispatchException = ex;
            }
            catch (Throwable err) {
                // As of 4.3, we're processing Errors thrown from handler methods as well,
                // making them available for @ExceptionHandler methods and other scenarios.
                dispatchException = new NestedServletException("Handler dispatch failed", err);
            }
            // 10.處理分發(fā)調(diào)用結(jié)果,如視圖模型解析、返回等工作   如果以上有任何錯誤,把錯誤信息封裝賦值給dispatchException 錯誤對象
            //如果dispatchException不為空的話,打印錯誤信息,如果ModelAndView返回的是一個頁面的話,會重新發(fā)起請求
            //如果沒有ModelAndView為空了 ,或者說controller返回不是一個頁面了, 執(zhí)行攔截器的后置方法,也是倒著遍歷
            //在這里面還干了一個一件事 ,那就是獲取到了 view視圖對象
            processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
        }
        catch (Exception ex) {
            triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
        }
        catch (Throwable err) {
            triggerAfterCompletion(processedRequest, response, mappedHandler,
                    new NestedServletException("Handler processing failed", err));
        }
        finally {
            if (asyncManager.isConcurrentHandlingStarted()) {
                // Instead of postHandle and afterCompletion
                if (mappedHandler != null) {
                    mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
                }
            }
            else {
                //整理由多部分請求使用的任何資源。
                // Clean up any resources used by a multipart request.
                if (multipartRequestParsed) {
                    cleanupMultipart(processedRequest);
                }
            }
        }
    }

聊一下比較重要的幾個方法

1.獲取處理器映射器

mappedHandler = getHandler(processedRequest);

在doDispatch方法中找這個方法,按住Ctrl鍵點擊進入

此一路千辛萬苦,一路封裝,滿載而歸

@Nullable
    protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        //handlerMappings中有5個對象
        //1. favconHandlerMapping 2.requestMappingHandlerMapping 3.beanNameHandlerMapping 4.resourceHandlerMapping 5.welcomePageHandlerMapping
        //遍歷這個五個對象
        if (this.handlerMappings != null) {
            for (HandlerMapping mapping : this.handlerMappings) {
                //當(dāng)遍歷到requestMappingHandlerMapping時  在requestMappingHandlerMapping中其實存儲了  所有攔截器的對象

                /**
                 * 這個方法一路千辛萬苦,一路封裝,滿載而歸
                 *  1.首先是調(diào)用AbstractHandlerMapping的getHandler方法,然后調(diào)用  AbstractHandlerMethodMapping的getHandlerInternal方法
                 *  然后重新創(chuàng)建創(chuàng)建對象 new HandlerMethod(this, handler) 把controller對象(從工廠中獲取) 賦值 給方法對象HandlerMethod
                 *  所以第一步就是: 把controller對象賦值給方法對象
                 *
                 *  2.然后調(diào)用AbstractHandlerMapping.getHandlerExecutionChain 轉(zhuǎn)換為 HandlerExecutionChain 對象
                 *  遍歷攔截器集合 把攔截器所有的對象賦值給HandlerExecutionChain對象的集合
                 *   所以第二步就是: 把方法對象轉(zhuǎn)換為HandlerExecutionChain對象并把所有的攔截器賦值到其中
                 *
                 */
                HandlerExecutionChain handler = mapping.getHandler(request);
                if (handler != null) {
                    return handler;
                }
            }
        }
        return null;
    }

我們再看一下

mapping.getHandler(request);方法

按住Ctrl鍵點擊進入,發(fā)現(xiàn)是HandlerMapping接口 , 按住快捷鍵 Ctrl + Alt + B 選擇AbstractHandlerMapping 這個實現(xiàn)類

@Override
    @Nullable
    public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
        //獲取Controller對象,但是獲取到時對應(yīng)方法的對象,方法對象中封裝有controller對象
        //一路獲取controller對象,把controller對象封裝進方法對象中,(前提是訪問的是controller中的方法)
        Object handler = getHandlerInternal(request);
        if (handler == null) {
            handler = getDefaultHandler();
        }
        if (handler == null) {
            return null;
        }
        // Bean name or resolved handler?
        if (handler instanceof String) {
            String handlerName = (String) handler;
            handler = obtainApplicationContext().getBean(handlerName);
        }

         //把方法對象和request傳進去 ,準(zhǔn)備把所有的攔截器封裝進 HandlerExecutionChain對象中
        HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request);

      // 現(xiàn)在 HandlerExecutionChain 對象中有 方法對象 而方法對象中存儲了controller對象, HandlerExecutionChain中有所有的攔截器對象
        if (logger.isTraceEnabled()) {
            logger.trace("Mapped to " + handler);
        }
        else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(DispatcherType.ASYNC)) {
            logger.debug("Mapped to " + executionChain.getHandler());
        }

        if (CorsUtils.isCorsRequest(request)) {
            CorsConfiguration globalConfig = this.corsConfigurationSource.getCorsConfiguration(request);
            CorsConfiguration handlerConfig = getCorsConfiguration(handler, request);
            CorsConfiguration config = (globalConfig != null ? globalConfig.combine(handlerConfig) : handlerConfig);
            executionChain = getCorsHandlerExecutionChain(request, executionChain, config);
        }

        return executionChain;
    }

2.獲取處理器適配器

HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

我們再回到 doDispatch方法中,找到該方法, 按住Ctrl鍵點擊進入

    //參數(shù)為對應(yīng)方法對象,或者是頁面資源對象
    protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
        //handlerAdapters 有三個適配器對象
        //1.RquestMappingHandlerAdapter  2.HttpRequestHandlerAdapter 3.SimpleControllerHandlerAdapter
        if (this.handlerAdapters != null) {
            for (HandlerAdapter adapter : this.handlerAdapters) {
               //查看哪個處理器符合請求需要的,返回符合條件的處理器
                if (adapter.supports(handler)) {
                    return adapter;
                }
            }
        }
        throw new ServletException("No adapter for handler [" + handler +
                "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
    }

3.獲取視圖ModelAndView

mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

我們再回到 doDispatch方法中,找到該方法, 點擊進入

發(fā)現(xiàn)是一個HandlerAdapter接口 ,按住快捷鍵 Ctrl + Alt + B ,選擇 AbstractHandlerMethodAdaper

按住Ctrl鍵點擊 handleInternal 方法,發(fā)現(xiàn)是一個抽象的方法,再次 按住快捷鍵 Ctrl + Alt + B 進入到RequestMappingHandlerAdapter

@Override
    protected ModelAndView handleInternal(HttpServletRequest request,
            HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {

        ModelAndView mav;
        checkRequest(request);

        //如果需要,在同步塊中執(zhí)行InvokehandlerMethod。
        // Execute invokeHandlerMethod in synchronized block if required.
        if (this.synchronizeOnSession) {
            HttpSession session = request.getSession(false);
            if (session != null) {
                Object mutex = WebUtils.getSessionMutex(session);
                synchronized (mutex) {
                    mav = invokeHandlerMethod(request, response, handlerMethod);
                }
            }
            else {
                // No HttpSession available -> no mutex necessary
                mav = invokeHandlerMethod(request, response, handlerMethod);
            }
        }
        else {
            // No synchronization on session demanded at all...
            //嘗試獲取ModelAndView對象,如果沒有HTML,則獲取不到視圖
            //這個方法里面執(zhí)行了controller的內(nèi)容
            mav = invokeHandlerMethod(request, response, handlerMethod);
        }

        if (!response.containsHeader(HEADER_CACHE_CONTROL)) {
            if (getSessionAttributesHandler(handlerMethod).hasSessionAttributes()) {
                applyCacheSeconds(response, this.cacheSecondsForSessionAttributeHandlers);
            }
            else {
                prepareResponse(response);
            }
        }
          //獲取到ModelAndView對象
        return mav;
    }

4.解析視圖

processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);

我們再回到 doDispatch方法中,找到該方法, 按住Ctrl鍵點擊進入

private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
            @Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
            @Nullable Exception exception) throws Exception {

        boolean errorView = false;

        if (exception != null) {
            if (exception instanceof ModelAndViewDefiningException) {
                logger.debug("ModelAndViewDefiningException encountered", exception);
                mv = ((ModelAndViewDefiningException) exception).getModelAndView();
            }
            else {
                Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
                mv = processHandlerException(request, response, handler, exception);
                errorView = (mv != null);
            }
        }

        //處理程序是否返回要呈現(xiàn)的視圖?
        // Did the handler return a view to render?
        if (mv != null && !mv.wasCleared()) {
            //這個方法當(dāng)中會獲取到視圖view
            render(mv, request, response);
            if (errorView) {
                WebUtils.clearErrorRequestAttributes(request);
            }
        }
        else {
            if (logger.isTraceEnabled()) {
                logger.trace("No view rendering, null ModelAndView returned.");
            }
        }

        if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
            // Concurrent handling started during a forward
            return;
        }

        if (mappedHandler != null) {
            //如果沒有ModelAndView為空了 ,或者說controller返回不是一個頁面了,執(zhí)行攔截器的后置方法,也是倒著遍歷
            mappedHandler.triggerAfterCompletion(request, response, null);
        }
    }

找到 render(mv, request, response); 方法, 按住Ctrl鍵點擊進入

protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // Determine locale for request and apply it to the response.
        Locale locale =
                (this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
        response.setLocale(locale);

        View view;
        String viewName = mv.getViewName();
        if (viewName != null) {
            //我們需要解析視圖名稱
            // We need to resolve the view name.
            //獲取到了視圖
            view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
            if (view == null) {
                throw new ServletException("Could not resolve view with name '" + mv.getViewName() +
                        "' in servlet with name '" + getServletName() + "'");
            }
        }
        else {
            // No need to lookup: the ModelAndView object contains the actual View object.
            view = mv.getView();
            if (view == null) {
                throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
                        "View object in servlet with name '" + getServletName() + "'");
            }
        }

        // Delegate to the View object for rendering.
        if (logger.isTraceEnabled()) {
            logger.trace("Rendering view [" + view + "] ");
        }
        try {
            if (mv.getStatus() != null) {
                response.setStatus(mv.getStatus().value());
            }
            //獲取到視圖之后
            view.render(mv.getModelInternal(), request, response);
        }
        catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Error rendering view [" + view + "]", ex);
            }
            throw ex;
        }
    }

這個方法中,解析視圖ModelAndView ,獲取View對象

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容