SpringMVC的核心流程
-
建立请求和Controller方法的映射集合的流程。
-
根据请求查找对应的Controller方法的流程。
-
请求参数绑定到方法形参,执行方法处理请求,返回结果进行视图渲染的流程。
HandlerMapping
HandlerMapping接口作用是将请求映射到处理程序,以及预处理和处理后的拦截器列表,映射是基于一些标准的,其中的细节因不同的实现而不相同。这是官方文档上一段描述,该接口只有一个方法getHandler(request),返回一个HandlerExecutionChain对象,接口本身很简单。
public interface HandlerMapping { String BEST_MATCHING_HANDLER_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingHandler"; String LOOKUP_PATH = HandlerMapping.class.getName() + ".lookupPath"; String PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE = HandlerMapping.class.getName() + ".pathWithinHandlerMapping"; String BEST_MATCHING_PATTERN_ATTRIBUTE = HandlerMapping.class.getName() + ".bestMatchingPattern"; String INTROSPECT_TYPE_LEVEL_MAPPING = HandlerMapping.class.getName() + ".introspectTypeLevelMapping"; String URI_TEMPLATE_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".uriTemplateVariables"; String MATRIX_VARIABLES_ATTRIBUTE = HandlerMapping.class.getName() + ".matrixVariables"; String PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE = HandlerMapping.class.getName() + ".producibleMediaTypes"; // 返回请求的一个处理程序handler和拦截器interceptors @Nullable HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception; }HandlerExecutionChain
HandlerExecutionChain 是Handler执行链,包含handler Object、interceptor。所以HandlerExecutionChain 提供了getHandler、getInterceptors方法,配置文件中配置的interceptor都会加入到HandlerExecutionChain。
public class HandlerExecutionChain { //handler实例 private final Object handler; //一组拦截器实例 @Nullable private HandlerInterceptor[] interceptors; //HandlerInterceptor的链表 @Nullable private List<HandlerInterceptor> interceptorList; ...... }AbstractHandlerMapping
从类图中可知,HandlerMapping主要由AbstractHandlerMapping实现。
public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport implements HandlerMapping, Ordered, BeanNameAware { @Override @Nullable public final HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception { //获取handler的具体逻辑,留给子类实现 Object handler = getHandlerInternal(request); // 如果获取到的handler为null,则采用默认的handler,即属性defaultHandler if (handler == null) { handler = getDefaultHandler(); } if (handler == null) { //如果连DefaultHandler也为空,则直接返回空 return null; } // Bean name or resolved handler? //如果handler是beanName,从容器里获取对应的bean if (handler instanceof String) { String handlerName = (String) handler; handler = obtainApplicationContext().getBean(handlerName); } //根据handler和request获取处理器链HandlerExecutionChain HandlerExecutionChain executionChain = getHandlerExecutionChain(handler, request); if (logger.isTraceEnabled()) { logger.trace("Mapped to " + handler); } else if (logger.isDebugEnabled() && !request.getDispatcherType().equals(DispatcherType.ASYNC)) { logger.debug("Mapped to " + executionChain.getHandler()); } if (hasCorsConfigurationSource(handler) || CorsUtils.isPreFlightRequest(request)) { CorsConfiguration config = (this.corsConfigurationSource != null ? this.corsConfigurationSource.getCorsConfiguration(request) : null); CorsConfiguration handlerConfig = getCorsConfiguration(handler, request); config = (config != null ? config.combine(handlerConfig) : handlerConfig); executionChain = getCorsHandlerExecutionChain(request, executionChain, config); } return executionChain; } }-
用户发起请求,会进入到AbstractHandlerMapping类中的getHandler方法中。
-
沿着方法的调用栈发现,请求先经过FrameworkServlet的service方法去处理。
-
在由HttpServlet的service方法转发到FrameworkServlet的doPost方法中。
-
doPost方法中会调用FrameworkServlet的processRequest方法。
-
processRequest方法会调用DispatcherServlet的doService方法。
-
doService方法会调用DispatcherServlet的doDispatch方法。
-
doDispatch方法会调用到DispatcherServlet的getHandler方法。
-
DispatcherServlet的getHandler方法会调用到AbstractHandlerMapping中的getHandler方法。
DispatcherServlet#doDispatch
public class DispatcherServlet extends FrameworkServlet { 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. // 根据当前request获取HandlerExecutionChain, // HandlerExecutionChain中包含了请求url,以及对应的controller以及controller中的方法 mappedHandler = getHandler(processedRequest); if (mappedHandler == null) { noHandlerFound(processedRequest, response); return; } // Determine handler adapter for the current request. // 通过HandlerExecutionChain获取对应的适配器,adapter负责完成参数解析 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); // Process last-modified header, if supported by the handler. //处理GET、HEAD请求的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; } } // 遍历所有定义的 interceptor,执行 preHandle 方法 if (!mappedHandler.applyPreHandle(processedRequest, response)) { return; } // Actually invoke the handler. // 调用目标Controller中的方法 mv = ha.handle(processedRequest, response, mappedHandler.getHandler()); if (asyncManager.isConcurrentHandlingStarted()) { return; } // 处理成默认视图名,也就是添加前缀和后缀等 applyDefaultViewName(processedRequest, mv); // 拦截器postHandle方法进行处理 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) { 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); } } } } }DispatcherServlet#getHandler
- 根据request信息uri找到对应HandlerExecutionChain执行链,HandlerExecutionChain中包含了请求url,以及对应的controller以及controller中的方法。
- 该方法会依次调用DispatcherServlet类中的成员变量List<HandlerMapping> handlerMappings中的HandlerMapping的实现类的getHandler方法。
handlerMappings中包含4个HandlerMapping的实现类
- requestMappingHandlerMapping -> RequestMappingHandlerMapping
- beanNameHandlerMapping -> BeanNameUrlHandlerMapping
- routerFunctionMapping -> RouterFunctionMapping
- defaultServletHandlerMapping -> SimpleUrlHandlerMapping
这里主要关注RequestMappingHandlerMapping
- 因为其他的HandlerMapping实现类并不会对主流的RequestMapping注解进行处理。
getHandlerInternal
- 从AbstractHandlerMapping的子类方法实现里获取到真正的Handler实例。
AbstractHandlerMethodMapping#lookupHandlerMethod
- 根据路径寻找Handler,并封装成 HandlerMethod
AbstractHandlerMapping#getHandlerExecutionChain
- 根据handler和request获取处理器链HandlerExecutionChain
到此,根据请求查找对应的Controller方法的流程就梳理完毕了。