springmvc-handlerInterceptor

HandlerInterceptor接口给我们提供了3个方法:

(1)preHandle: 在执行controller处理之前执行,返回值为boolean ,返回值为true时接着执行postHandle和afterCompletion,如果我们返回false则中断执行
(2)postHandle:在执行controller的处理后,在ModelAndView处理前执行
(3)afterCompletion :在DispatchServlet执行完ModelAndView之后执行

源码

public interface HandlerInterceptor {  


     /**  
     * preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,SpringMVC中的Interceptor拦截器是链式的,可以同时存在  
     * 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行,而且所有的Interceptor中的preHandle方法都会在  
     * Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的,这种中断方式是令preHandle的返  
     * 回值为false,当preHandle的返回值为false的时候整个请求就结束了。  
     */    
    boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  
        throws Exception;  


    /**  
     * 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,它的执行时间是在处理器进行处理之  
     * 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行,也就是说在这个方法中你可以对ModelAndView进行操  
     * 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用,这跟Struts2里面的拦截器的执行过程有点像,  
     * 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor  
     * 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前,要在Interceptor之后调用的内容都写在调用invoke方法之后。  
     */  
    void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)  
            throws Exception;  


    /**  
     * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图执行,  
     * 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。  
     */   
    void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  
            throws Exception;  

}  

SpringMVC还提供了HandlerInterceptorAdapter 其是抽象类,也是HandlerInterceptor的子类,在实现了HandlerInterceptor的三个函数后还增加了一个函数。
(1)preHandle: 在执行controller处理之前执行,返回值为boolean ,返回值为true时接着执行postHandle和afterCompletion,如果我们返回false则中断执行
(2)postHandle:在执行controller的处理后,在ModelAndView处理前执行
(3)afterCompletion :在DispatchServlet执行完ModelAndView之后执行
(4)afterConcurrentHandlingStarted:这个方法会在Controller方法异步执行时开始执行,而Interceptor的postHandle方法则是需要等到Controller的异步执行完才能执行,只要继承这个类并实现其方法就可以了。

我们可以看到这三个方法的调用过程是不一样的,接下来我们分析一下这个三个方法具体调用实现的地方。其最终实现调用的地方是在doDispatch函数中,因为doDispatch完成了一个请求到返回数据的完整操作。

protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

        HandlerExecutionChain mappedHandler = null;  

        .......  

        try {  
            ModelAndView mv = null;  
            Exception dispatchException = null;  

            try {  

                .......  
                //获取HandlerExecutionChain  
                mappedHandler = getHandler(processedRequest);  

                ......  
                //最终会调用HandlerInterceptor的preHandle方法  
                if (!mappedHandler.applyPreHandle(processedRequest, response)) {  
                    return;  
                }  
<span style="white-space:pre">// </span>调用具体的Controller中的处理方法<span style="white-space:pre">Actually invoke the handler.  
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());</span>  
                ......  
                //最终会调用HandlerInterceptor的postHandle方法  
                mappedHandler.applyPostHandle(processedRequest, response, mv);  
            }  
            catch (Exception ex) {  
                dispatchException = ex;  
            }  
            processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);  
        }  
        catch (Exception ex) {  
            //最终会调用HandlerInterceptor的afterCompletion 方法  
            triggerAfterCompletion(processedRequest, response, mappedHandler, ex);  
        }  
        catch (Error err) {  
            //最终会调用HandlerInterceptor的afterCompletion 方法  
            triggerAfterCompletionWithError(processedRequest, response, mappedHandler, 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);  
                }  
            }  
        }  
    }  

mappedHandler.applyPreHandle(processedRequest, response):最终会调用HandlerInterceptor的preHandle方法。在HandlerExecutionChain中的具体实现如下,我们可以看到会调用所有的HandlerInterceptor拦截器并调用其preHandler方法

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)) {//分别调用拦截器的preHandle方法  
                    triggerAfterCompletion(request, response, null);  
                    return false;  
                }  
                this.interceptorIndex = i;  
            }  
        }  
        return true;  
    }  

mappedHandler.applyPostHandle(processedRequest, response, mv):最终会调用HandlerInterceptor的postHandle方法

具体实现是在HandlerExecutionChain中实现如下,就是获取所有的拦截器并调用其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);//分别调用拦截器的postHandle方法  
            }  
        }  
    }  

triggerAfterCompletion(processedRequest, response, mappedHandler, ex):最终会调用HandlerInterceptor的afterCompletion 方法
triggerAfterCompletionWithError(processedRequest, response, mappedHandler, err):最终会调用HandlerInterceptor的afterCompletion 方法

private void triggerAfterCompletionWithError(HttpServletRequest request, HttpServletResponse response,  
            HandlerExecutionChain mappedHandler, Error error) throws Exception {  

        ServletException ex = new NestedServletException("Handler processing failed", error);  
        if (mappedHandler != null) {  
            mappedHandler.triggerAfterCompletion(request, response, ex);  
        }  
        throw ex;  
    }  

1
2
3
4
5
6
7
8
9
private void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response,
HandlerExecutionChain mappedHandler, Exception ex) throws Exception {

        if (mappedHandler != null) {  
            mappedHandler.triggerAfterCompletion(request, response, ex);  
        }  
        throw ex;  
    }  
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);//调用拦截器的afterCompletion方法  
                }  
                catch (Throwable ex2) {  
                    logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);  
                }  
            }  
        }  
    }  

通过以上代码分析我们可以看到HandlerInterceptor拦截器的最终调用实现是在DispatcherServlet的doDispatch方法中,并且SpringMVC提供了HandlerExecutionChain来帮助我们执行所有配置的HandlerInterceptor拦截器,并分别调用HandlerInterceptor所提供的方法。转载自:https://blog.csdn.net/fengyuhan123/article/details/79667698

posted @ 2022-11-03 15:52  wiselee/  阅读(76)  评论(0编辑  收藏  举报