DiapatcherServlet的继承树![SpringDiapatcherServlet的继承树](https://img-blog.csdnimg.cn/20210207101709316.png)
执行调用
org.springframework.web.servlet.FrameworkServlet.processRequest(HttpServletRequest, HttpServletResponse)
/**
* Process this request, publishing an event regardless of the outcome. 处理此请求,发布事件,而不管结果如何。
* <p>The actual event handling is performed by the abstract <p> 实际的事件处理是由抽象的模板方法执行的
* {@link #doService} template method.
*/
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
// 国际化功能
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContext localeContext = buildLocaleContext(request);
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
initContextHolders(request, localeContext, requestAttributes);
try {
doService(request, response); // 在DispatcherServlet 中实现 然后调用 doDispatcherServlet 方法
}
catch (ServletException | IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}
finally {
resetContextHolders(request, previousLocaleContext, previousAttributes);
if (requestAttributes != null) {
requestAttributes.requestCompleted();
}
logResult(request, response, failureCause, asyncManager);
publishRequestHandledEvent(request, response, startTime, failureCause);
}
}
org.springframework.web.servlet.DispatcherServlet.doDispatch(HttpServletRequest,HttpServletResponse)
/**
* Process the actual dispatching to the handler.
* <p>The handler will be obtained by applying the servlet's HandlerMappings in order.
* The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters
* to find the first that supports the handler class.
* <p>All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers
* themselves to decide which methods are acceptable.
* @param request current HTTP request
* @param response current HTTP response
* @throws Exception in case of any kind of processing failure
*/
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 {
// checkMultipart 的作用:Convert the request into a multipart request, and make multipart resolver available. If no multipart resolver is set, simply use the existing request.
// 将请求转换为多部分请求,并使多部分解析器可用。 如果没有设置多部分解析器,使用现有的请求。
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// 确定当前请求的handle 和 handler的执行链
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
// mappedHandler 中的参数在图1.2中
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
获取mappedHandler
> /**
* Return the HandlerExecutionChain for this request.
* <p>Tries all handler mappings in order.
* @param request current HTTP request
* @return the HandlerExecutionChain, or {@code null} if no handler could be found
*/
@Nullable
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
// handlermappings是springmvc初始化后得出来的
// 相关解释下图 1.1
if (this.handlerMappings != null) {
for (HandlerMapping mapping : this.handlerMappings) {
HandlerExecutionChain handler = mapping.getHandler(request);
if (handler != null) {
return handler;
}
}
}
return null;
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (mappedHandler == null || mappedHandler.getHandler() == null) {
// 没有找到处理请求的handle 去注释和报错 NoHandlerFoundException
noHandlerFound(processedRequest, response);
return;
}
// 确定在handle中该请求对应的处理器 可以解析对应请求的参数 和 相应数据
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/**
* Return the HandlerAdapter for this handler object.
* @param handler the handler object to find an adap