對HandlerExecutionChain類的理解分析


HandlerExecutionChain類比較簡單,好理解。

========================================================================

/*
* 處理器執行鏈由處理器對象和攔截器組成。
*/
public
class HandlerExecutionChain {

========================================================================

下面是類的部分屬性。

    private final Object handler;  //處理器對象。

    private HandlerInterceptor[] interceptors; //攔截器數組

    private List<HandlerInterceptor> interceptorList; //攔截器列表

 

========================================================================

    /**
     * Apply preHandle methods of registered interceptors.
     * @return {@code true} if the execution chain should proceed with the
     * next interceptor or the handler itself. Else, DispatcherServlet assumes
     * that this interceptor has already dealt with the response itself.
* 執行已經注冊的攔截的 preHandle()方法。如果返回true,則執行鏈可以執行下一個攔截器的preHandle()方法或 handler 自身。
* 否則,
*/ boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, this.handler)) { triggerAfterCompletion(request, response, null); return false; } this.interceptorIndex = i; } } return true; }

 

========================================================================

    /*
* 執行已經注冊的攔截器 postHandle()方法。
*/ void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception { HandlerInterceptor[] interceptors = getInterceptors(); if (!ObjectUtils.isEmpty(interceptors)) { for (int i = interceptors.length - 1; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; interceptor.postHandle(request, response, this.handler, mv); } } }

 

========================================================================

    /**
     * 這個方法只會執行preHandle()方法已經成功執行並且返回true的攔截器中的postHandle()方法。 */
    void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)
            throws Exception {

        HandlerInterceptor[] interceptors = getInterceptors();
        if (!ObjectUtils.isEmpty(interceptors)) {
            for (int i = this.interceptorIndex; i >= 0; i--) {
                HandlerInterceptor interceptor = interceptors[i];
                try {
                    interceptor.afterCompletion(request, response, this.handler, ex);
                }
                catch (Throwable ex2) {
                    logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
                }
            }
        }
    }

 

========================================================================

========================================================================

========================================================================

========================================================================

========================================================================

========================================================================

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM