SpringMVC源码分析

DispatchServlet

  众所周知,SpringMVC的核心Servlet是DispatchServlet,于是找到它的关键方法doDispatch来进行深入地分析:

doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {

		**//检查是否携带文件上传(request中会带有multipart信息)**
		processedRequest = checkMultipart(request);
		**//最重要的一个方法:找到对应的Controller,并且执行相应的方法。
		mappedHandler = getHandler(processedRequest);
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
				String method = request.getMethod();
				// **执行找到的Handler的具体方法
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
				applyDefaultViewName(processedRequest, mv);
				mappedHandler.applyPostHandle(processedRequest, response, mv);
			}
			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
		}

		}
	}
  • getHandler方法调用链:HandlerExecutionChain handler = hm.getHandler(request);--->Object handler = getHandlerInternal(request);--->AbstractHandlerMethodMapping的HandlerMethod handlerMethod = lookupHandlerMethod(lookupPath, request);--->List directPathMatches = this.mappingRegistry.getMappingsByUrl(lookupPath);
  • 最后发现其实和模拟SpringMVC的方式差不多,都是从一张表中取出对应关系。
  • 那么这张表是什么时候初始化的呢?初始化的时候就用到了之前学到的Spring的扩展点InitializingBean接口的afterPropertiesSet方法进行表的初始化。(AbstractHandlerMethodMapping)
posted @ 2021-10-21 21:40  莫西123  阅读(32)  评论(0)    收藏  举报