SpringMVC源码解析-HandlerExecutionChain

最终会调用HandlerInterceptor的

preHandle

调用所有的HandlerInterceptor拦截器并调用其preHandler方法。

applyPostHandle

获取所有的拦截器并调用其postHandle方法。

triggerAfterCompletion

触发afterCompletion执回调的映射HandlerInterceptors。 只会调用afterCompletion执行对于其preHandle调用已成功完成并返回true的拦截器

	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);
				}
			}
		}
	}

HandlerInterceptor拦截器的最终调用实现是在DispatcherServlet的doDispatch方法中
因此,SpringMVC提供了HandlerExecutionChain来帮助我们执行所有配置的HandlerInterceptor拦截器,并分别调用HandlerInterceptor所提供的方法。

猜你喜欢

转载自blog.csdn.net/qq_33589510/article/details/106565459