1 Handler⽅法的执⾏时机
1.1 打断点
1.2 观察调用栈
左下角的调用栈,顶行是断点处代码,下面每一行是都是上行方法的调用点。
doDispathch⽅法中的1064⾏代码完成handler⽅法的调⽤
2 ⻚⾯渲染时机
2.1 打断点
2.2 观察调用栈

调用的doDispatch方法,该方法又调用了processDispatchResult方法。
3 SpringMVC处理请求的流程
SpringMVC处理请求的流程即为org.springframework.web.servlet.DispatcherServlet#doDispatch⽅法的执⾏过程,其中步骤 2、3、4、5是核⼼步骤
- 调⽤getHandler()获取到能够处理当前请求的执⾏链 HandlerExecutionChain(Handler+拦截器)
但是如何去getHandler的?后⾯进⾏分析
- 调⽤getHandlerAdapter();获取能够执⾏1)中Handler的适配器
但是如何去getHandlerAdapter的?后⾯进⾏分析
- 适配器调⽤Handler执⾏ha.handle(总会返回⼀个ModelAndView对象)
调⽤processDispatchResult()⽅法完成视图渲染跳转
protected void doDispatch(HttpServletRequest request, HttpServletResponse response)throws Exception {HttpServletRequest processedRequest = request;HandlerExecutionChain mappedHandler = null;boolean multipartRequestParsed = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv = null;Exception dispatchException = null;try {// 1 检查是否是文件上传的请求processedRequest = checkMultipart(request);multipartRequestParsed = (processedRequest != request);// Determine handler for the current request./*2 取得处理当前请求的Controller,这里也称为Handler,即处理器这里并不是直接返回 Controller,而是返回 HandlerExecutionChain请求处理链对象,该对象封装了Handler和Inteceptor*/mappedHandler = getHandler(processedRequest);if (mappedHandler == null) {// 如果 handler 为空,则返回404noHandlerFound(processedRequest, response);return;}// Determine handler adapter for the current request.// 3 获取处理请求的处理器适配器 HandlerAdapterHandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());// Process last-modified header, if supported by the handler.// 处理 last-modified 请求头String method = request.getMethod();boolean isGet = "GET".equals(method);if (isGet || "HEAD".equals(method)) {long lastModified = ha.getLastModified(request,mappedHandler.getHandler());if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {return;}}if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// Actually invoke the handler.// 4 实际处理器处理请求,返回结果视图对象mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}// 结果视图对象的处理applyDefaultViewName(processedRequest, mv);mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {dispatchException = ex;}catch (Throwable err) {// As of 4.3, we're processing Errors thrown from handler methods as well,// making them available for @ExceptionHandler methods and other scenarios.dispatchException = new NestedServletException("Handler dispatch failed",err);}processDispatchResult(processedRequest, response, mappedHandler,mv, dispatchException);}catch (Exception ex) {//最终会调用HandlerInterceptor的afterCompletion 方法triggerAfterCompletion(processedRequest, response, mappedHandler, ex);}catch (Throwable err) {//最终会调用HandlerInterceptor的afterCompletion 方法triggerAfterCompletion(processedRequest, response, mappedHandler,new NestedServletException("Handler processing failed",err));}finally {if (asyncManager.isConcurrentHandlingStarted()) {// Instead of postHandle and afterCompletionif (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest,response);}}else {// Clean up any resources used by a multipart request.if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}}}
