1 Handler⽅法的执⾏时机

1.1 打断点

在handler方法内第一行打断点
一、重要时机点 - 图1

1.2 观察调用栈

左下角的调用栈,顶行是断点处代码,下面每一行是都是上行方法的调用点。
image.png
doDispathch⽅法中的1064⾏代码完成handler⽅法的调⽤
一、重要时机点 - 图3

2 ⻚⾯渲染时机

2.1 打断点

image.png

2.2 观察调用栈

image.png
调用的doDispatch方法,该方法又调用了processDispatchResult方法。

3 SpringMVC处理请求的流程

SpringMVC处理请求的流程即为org.springframework.web.servlet.DispatcherServlet#doDispatch⽅法的执⾏过程,其中步骤 2、3、4、5是核⼼步骤

  1. 调⽤getHandler()获取到能够处理当前请求的执⾏链 HandlerExecutionChain(Handler+拦截器)

但是如何去getHandler的?后⾯进⾏分析

  1. 调⽤getHandlerAdapter();获取能够执⾏1)中Handler的适配器

但是如何去getHandlerAdapter的?后⾯进⾏分析

  1. 适配器调⽤Handler执⾏ha.handle(总会返回⼀个ModelAndView对象)
  2. 调⽤processDispatchResult()⽅法完成视图渲染跳转

    1. protected void doDispatch(HttpServletRequest request, HttpServletResponse response)
    2. 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. // 1 检查是否是文件上传的请求
    12. processedRequest = checkMultipart(request);
    13. multipartRequestParsed = (processedRequest != request);
    14. // Determine handler for the current request.
    15. /*
    16. 2 取得处理当前请求的Controller,这里也称为Handler,即处理器
    17. 这里并不是直接返回 Controller,而是返回 HandlerExecutionChain
    18. 请求处理链对象,该对象封装了Handler和Inteceptor
    19. */
    20. mappedHandler = getHandler(processedRequest);
    21. if (mappedHandler == null) {
    22. // 如果 handler 为空,则返回404
    23. noHandlerFound(processedRequest, response);
    24. return;
    25. }
    26. // Determine handler adapter for the current request.
    27. // 3 获取处理请求的处理器适配器 HandlerAdapter
    28. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
    29. // Process last-modified header, if supported by the handler.
    30. // 处理 last-modified 请求头
    31. String method = request.getMethod();
    32. boolean isGet = "GET".equals(method);
    33. if (isGet || "HEAD".equals(method)) {
    34. long lastModified = ha.getLastModified(request,
    35. mappedHandler.getHandler());
    36. if (new ServletWebRequest(request, response)
    37. .checkNotModified(lastModified) && isGet) {
    38. return;
    39. }
    40. }
    41. if (!mappedHandler.applyPreHandle(processedRequest, response)) {
    42. return;
    43. }
    44. // Actually invoke the handler.
    45. // 4 实际处理器处理请求,返回结果视图对象
    46. mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    47. if (asyncManager.isConcurrentHandlingStarted()) {
    48. return;
    49. }
    50. // 结果视图对象的处理
    51. applyDefaultViewName(processedRequest, mv);
    52. mappedHandler.applyPostHandle(processedRequest, response, mv);
    53. }
    54. catch (Exception ex) {
    55. dispatchException = ex;
    56. }
    57. catch (Throwable err) {
    58. // As of 4.3, we're processing Errors thrown from handler methods as well,
    59. // making them available for @ExceptionHandler methods and other scenarios.
    60. dispatchException = new NestedServletException("Handler dispatch failed",
    61. err);
    62. }
    63. processDispatchResult(processedRequest, response, mappedHandler,
    64. mv, dispatchException);
    65. }
    66. catch (Exception ex) {
    67. //最终会调用HandlerInterceptor的afterCompletion 方法
    68. triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
    69. }
    70. catch (Throwable err) {
    71. //最终会调用HandlerInterceptor的afterCompletion 方法
    72. triggerAfterCompletion(processedRequest, response, mappedHandler,
    73. new NestedServletException("Handler processing failed",
    74. err));
    75. }
    76. finally {
    77. if (asyncManager.isConcurrentHandlingStarted()) {
    78. // Instead of postHandle and afterCompletion
    79. if (mappedHandler != null) {
    80. mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest,
    81. response);
    82. }
    83. }
    84. else {
    85. // Clean up any resources used by a multipart request.
    86. if (multipartRequestParsed) {
    87. cleanupMultipart(processedRequest);
    88. }
    89. }
    90. }
    91. }