转自:https://blog.csdn.net/qq924862077/article/details/53524507
对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;
- }
- public class MyInterceptor implements HandlerInterceptor {
- /**
- * @Title: preHandle
- * @Description: 在执行controller之前运行
- * @param request
- * @param response
- * @param handler
- * @return
- * @throws Exception
- */
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- return true;
- }
- /**
- * @Title: postHandle
- * @Description: 在执行完controller之后,ModelAndView渲染之前开始运行
- * @param request
- * @param response
- * @param handler
- * @param modelAndView
- * @throws Exception
- */
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
- throws Exception {
- }
- /**
- * @Title: afterCompletion
- * @Description: 该方法将在整个请求完成之后,也就是DispatcherServlet渲染了视图后执行
- * @param request
- * @param response
- * @param handler
- * @param ex
- * @throws Exception
- */
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- }
- }
(1)preHandle: 在执行controller处理之前执行,返回值为boolean ,返回值为true时接着执行postHandle和afterCompletion,如果我们返回false则中断执行
(2)postHandle:在执行controller的处理后,在ModelAndView处理前执行
(3)afterCompletion :在DispatchServlet执行完ModelAndView之后执行
(4)afterConcurrentHandlingStarted:这个方法会在Controller方法异步执行时开始执行,而Interceptor的postHandle方法则是需要等到Controller的异步执行完才能执行,只要继承这个类并实现其方法就可以了。
- public abstract class HandlerInterceptorAdapter implements AsyncHandlerInterceptor {
- /**
- * This implementation always returns {@code true}.
- */
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception {
- return true;
- }
- /**
- * This implementation is empty.
- */
- @Override
- public void postHandle(
- HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
- throws Exception {
- }
- /**
- * This implementation is empty.
- */
- @Override
- public void afterCompletion(
- HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- }
- /**
- * This implementation is empty.
- */
- @Override
- public void afterConcurrentHandlingStarted(
- HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception {
- }
- }
AsyncHandlerInterceptor也是一个接口,提供了afterConcurrentHandlingStarted方法定义
- public interface AsyncHandlerInterceptor extends HandlerInterceptor {
- /**
- * Called instead of {@code postHandle} and {@code afterCompletion}, when
- * the a handler is being executed concurrently.
- * <p>Implementations may use the provided request and response but should
- * avoid modifying them in ways that would conflict with the concurrent
- * execution of the handler. A typical use of this method would be to
- * clean up thread-local variables.
- *
- * @param request the current request
- * @param response the current response
- * @param handler the handler (or {@link HandlerMethod}) that started async
- * execution, for type and/or instance examination
- * @throws Exception in case of errors
- */
- void afterConcurrentHandlingStarted(
- HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception;
- }