SpringMVC拦截器执行流程

1:MyInterceptor1、MyInterceptor2这2个拦截器都放行

MyInterceptor1......preHandle
MyInterceptor2......preHandle

MyInterceptor2......postHandle
MyInterceptor1......postHandle

MyInterceptor2......afterCompletion
MyInterceptor1......afterCompletion

preHandle执行顺序和拦截器放置顺序一致;postHandle、afterCompletion执行顺序和拦截器放置顺序相反


2:MyInterceptor1放行、MyInterceptor2不放行

MyInterceptor1......preHandle
MyInterceptor2......preHandle
MyInterceptor1......afterCompletion

MyInterceptor2不放行,MyInterceptor2的postHandle、afterCompletion方法不会执行
只要有一个拦截器不放行,postHandle方法不会执行


3:MyInterceptor1、MyInterceptor2这2个拦截器都不放行

MyInterceptor1......preHandle

MyInterceptor1不放行,MyInterceptor1的postHandle、afterCompletion方法不会执行
MyInterceptor1不放行,MyInterceptor2不会执行


若系统中有统一日志记录处理,应该将日志处理的拦截器放在第一个位置,并且必须放行,这样才能保证afterCompletion方法会执行
(若其它拦截器不放行,会导致postHandle方法不执行)

若系统中有登录校验拦截器、权限校验拦截器
应该把登录校验拦截器放在权限校验拦截器的前面,因为只有登录通过了,才会进行权限校验


单个拦截器执行流程:


不放行
preHandle------>返回


放行
preHandle------>handler中方法------>postHandle------>handler中方法返回ModelAndView------>afterCompletion

猜你喜欢

转载自www.cnblogs.com/thaipine/p/11650997.html