Java培训之多个拦截方法的执行顺序

  • 关于执行顺序

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – preHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – preHandle

************************************biz method*******************************

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – postHandle

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – postHandle

com.atguigu.springmvc.interceptors.SecondHandlerInterceptor – afterCompletion

com.atguigu.springmvc.interceptors.FirstHandlerInterceptor – afterCompletion

  • 执行顺序图解

java培训

  • 从源代码的执行角度分析流程:

boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {

if (getInterceptors() != null) {

for (int i = 0; i < getInterceptors().length; i++) {

HandlerInterceptor interceptor = getInterceptors()[i];

if (!interceptor.preHandle(request, response, this.handler)) {

triggerAfterCompletion(request, response, null);

return false;

}

this.interceptorIndex = i;

}

}

return true;

}

java培训

void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {

if (getInterceptors() == null) {

return;

}

for (int i = getInterceptors().length – 1; i >= 0; i–) {

HandlerInterceptor interceptor = getInterceptors()[i];

interceptor.postHandle(request, response, this.handler, mv);

}

}

java培训

void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Exception ex)

throws Exception {

 

if (getInterceptors() == null) {

return;

}

for (int i = this.interceptorIndex; i >= 0; i–) {

HandlerInterceptor interceptor = getInterceptors()[i];

try {

interceptor.afterCompletion(request, response, this.handler, ex);

}

catch (Throwable ex2) {

logger.error(“HandlerInterceptor.afterCompletion threw exception”, ex2);

}

}

}

java培训

java培训

  • 源码分析:分析interceptorIndex的值情况

java培训


上一篇:
下一篇:
关于尚硅谷
教育理念
名师团队
学员心声
资源下载
视频下载
资料下载
工具下载
加入我们
招聘岗位
岗位介绍
招贤纳师
联系我们
全国统一咨询电话:010-56253825
地址:北京市昌平区宏福科技园综合楼6层(北京校区)

深圳市宝安区西部硅谷大厦B座C区一层(深圳校区)

上海市松江区谷阳北路166号大江商厦6层(上海校区)

武汉市东湖高新开发区东湖网谷(武汉校区)