在查看SpringMvc DispatcherServelt 源码中会发现
HandlerExecutionChain 类中
//类在创建的时候,自动加载了HandlerInterceptor 接口实现类,构建成interceptors List集合
publicHandlerExecutionChain(Object handler,HandlerInterceptor[] interceptors) {
if(handler instanceofHandlerExecutionChain) {
HandlerExecutionChain originalChain = (HandlerExecutionChain) handler;this.handler = originalChain.getHandler();this.interceptorList = newArrayList();CollectionUtils.mergeArrayIntoCollection(originalChain.getInterceptors(), this.interceptorList);CollectionUtils.mergeArrayIntoCollection(interceptors, this.interceptorList);} else{
this.handler = handler;this.interceptors = interceptors;}
}
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;
}
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);
}
}
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);
}
}
}
在上面3个方法中循环,调用了HandlerInterceptor 实现类中 preHandle,postHandle,afterCompletion,中的3个方法
以下是我的测试
importorg.apache.logging.log4j.LogManager;importorg.apache.logging.log4j.Logger;importorg.springframework.web.servlet.HandlerInterceptor;importorg.springframework.web.servlet.ModelAndView;
importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;
public classZdyHandlerInterceptor implementsHandlerInterceptor {
Logger log= LogManager.getLogger(ZdyHandlerInterceptor.class.getName());
//整个请求结束之后,modelView已完成渲染,进行的操作例如回收资源@Overridepublic voidafterCompletion(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,Exception arg3)
throwsException {
log.info("后置拦截器");//TODO Auto-generated method stub}
//在Controller处理业务之后,返回modelView,但还没对前端进行渲染操作,也就是说在此方法中我们可以对返回的modelView做些操作@Overridepublic voidpostHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2,ModelAndView arg3)
throwsException {
//TODO Auto-generated method stublog.info("处理器");}
//前置拦截器,默认返回false,只有返回true的时候才会执行后面的两个方法@Overridepublic booleanpreHandle(HttpServletRequest arg0,HttpServletResponse arg1,Object arg2) throwsException {
//TODO Auto-generated method stublog.info("前置拦截器");return false;}
}
SpringMvc配置文件
测试结果
INFO -> (http-nio-9080-exec-7) - 前置拦截器
preHandle 改为true后
INFO -> (http-nio-9080-exec-10) - 前置拦截器 INFO -> (http-nio-9080-exec-10) - 处理器 INFO ->(http-nio-9080-exec-10) - 后置拦截器