SpringMVC通过url请求到Controller的过程
>org.springframework.web.servlet.FrameworkServlet#doGet
>org.springframework.web.servlet.FrameworkServlet#processRequest
>org.springframework.web.servlet.DispatcherServlet#doService
>org.springframework.web.servlet.DispatcherServlet#doDispatch
>org.springframework.web.servlet.DispatcherServlet#getHandler //找到处理当前请求的handler
>org.springframework.web.servlet.handler.AbstractHandlerMapping#getHandler //获取HandlerExecutionChain
>org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#getHandlerInternal
>org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#lookupHandlerMethod //查找一个最合适的handler method
>org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.MappingRegistry#getMappingsByUrl //获取到url中的controller访问的 patternsCondition
>org.springframework.web.servlet.handler.AbstractHandlerMethodMapping#addMatchingMappings
//matches.add(new Match(match,this.mappingRegistry.getMappings().get(mapping))); Match(pattern,controller)
//最终返回一个最合适的handler
>org.springframework.web.servlet.DispatcherServlet#getHandlerAdapter //找到处理当前请求的handler adapter
>org.springframework.web.servlet.HandlerAdapter#handle //使用handler去处理请求
>org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#handleInternal
>org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#invokeHandlerMethod
>org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod#invokeAndHandle
>org.springframework.web.method.support.InvocableHandlerMethod#invokeForRequest
>org.springframework.web.method.support.InvocableHandlerMethod#doInvoke
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 {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request. 找到处理当前请求的handler
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request. 找到处理当前请求的handler adapter
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (logger.isDebugEnabled()) {
logger.debug("Last-Modified value for [" + getRequestUri(request) + "] is: " + lastModified);
}
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler. 实际调用的handler,使用handler去处理请求
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, were 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) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}