• 1、根据当前请求,找到可以处理请求的handler以及handler的所有拦截器
  • 2、先来循序执行所有拦截器的preHandle方法
    • 1、如果当前preHandle返回true,则执行下一个拦截器的preHandle方法
    • 2、如果当前preHandle返回false,则倒序执行afterCompletion方法
  • 3、如果任何一个拦截器返回false,直接跳出不执行目标方法
  • 4、所有拦截器都返回true,执行目标方法
  • 5、倒序执行所有拦截器的postHandle方法。
  • 6、前面的步骤有任何异常都会直接触发afterCompletion
  • 7、最终在页面渲染完成之后会倒序执行拦截器afterCompletion方法

image.png

执行处源码DispatchServlet

  1. @SuppressWarnings("deprecation")
  2. protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
  3. HttpServletRequest processedRequest = request;
  4. HandlerExecutionChain mappedHandler = null;
  5. boolean multipartRequestParsed = false;
  6. WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
  7. try {
  8. ModelAndView mv = null;
  9. Exception dispatchException = null;
  10. try {
  11. processedRequest = checkMultipart(request);
  12. multipartRequestParsed = (processedRequest != request);
  13. // Determine handler for the current request.
  14. mappedHandler = getHandler(processedRequest);
  15. if (mappedHandler == null) {
  16. noHandlerFound(processedRequest, response);
  17. return;
  18. }
  19. // Determine handler adapter for the current request.
  20. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
  21. // Process last-modified header, if supported by the handler.
  22. String method = request.getMethod();
  23. boolean isGet = HttpMethod.GET.matches(method);
  24. if (isGet || HttpMethod.HEAD.matches(method)) {
  25. long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
  26. if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
  27. return;
  28. }
  29. }
  30. if (!mappedHandler.applyPreHandle(processedRequest, response)) {
  31. return;
  32. }
  33. // Actually invoke the handler.
  34. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
  35. if (asyncManager.isConcurrentHandlingStarted()) {
  36. return;
  37. }
  38. applyDefaultViewName(processedRequest, mv);
  39. mappedHandler.applyPostHandle(processedRequest, response, mv);
  40. }
  41. catch (Exception ex) {
  42. dispatchException = ex;
  43. }
  44. catch (Throwable err) {
  45. // As of 4.3, we're processing Errors thrown from handler methods as well,
  46. // making them available for @ExceptionHandler methods and other scenarios.
  47. dispatchException = new NestedServletException("Handler dispatch failed", err);
  48. }
  49. processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
  50. }
  51. catch (Exception ex) {
  52. triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
  53. }
  54. catch (Throwable err) {
  55. triggerAfterCompletion(processedRequest, response, mappedHandler,
  56. new NestedServletException("Handler processing failed", err));
  57. }
  58. finally {
  59. if (asyncManager.isConcurrentHandlingStarted()) {
  60. // Instead of postHandle and afterCompletion
  61. if (mappedHandler != null) {
  62. mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
  63. }
  64. }
  65. else {
  66. // Clean up any resources used by a multipart request.
  67. if (multipartRequestParsed) {
  68. cleanupMultipart(processedRequest);
  69. }
  70. }
  71. }
  72. }
  1. boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
  2. for (int i = 0; i < this.interceptorList.size(); i++) {
  3. HandlerInterceptor interceptor = this.interceptorList.get(i);
  4. if (!interceptor.preHandle(request, response, this.handler)) {
  5. triggerAfterCompletion(request, response, null);
  6. return false;
  7. }
  8. this.interceptorIndex = i;
  9. }
  10. return true;
  11. }
  1. void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, @Nullable Exception ex) {
  2. for (int i = this.interceptorIndex; i >= 0; i--) {
  3. HandlerInterceptor interceptor = this.interceptorList.get(i);
  4. try {
  5. interceptor.afterCompletion(request, response, this.handler, ex);
  6. }
  7. catch (Throwable ex2) {
  8. logger.error("HandlerInterceptor.afterCompletion threw exception", ex2);
  9. }
  10. }
  11. }
  1. /**
  2. * Apply postHandle methods of registered interceptors.
  3. */
  4. void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
  5. throws Exception {
  6. for (int i = this.interceptorList.size() - 1; i >= 0; i--) {
  7. HandlerInterceptor interceptor = this.interceptorList.get(i);
  8. interceptor.postHandle(request, response, this.handler, mv);
  9. }
  10. }