注: SpringFramework的版本是4.3.x。java
图1 DispatcherServlet的doService方法时序图git
通常状况下,咱们不会本身指定HandlerAdapter,因此只须要分析默认的状况。首先看DispatcherServlet的方法initHandlerAdapters(ApplicationContext context),该方法中加载文件DispatcherServlet.properties中指定的HandlerAdapter,以下图2所示,默认的HandlerAdapter有HttpRequestHandlerAdapter、SimpleControllerHandlerAdapter、AnnotationMethodHandlerAdapter。github
图2 DispatcherServlet.properties指定的HandlerAdapterweb
HttpRequestHandlerAdapter类在spring-webmvc模块,类图以下图3所示,较为简单。spring
图3 HttpRequestHandlerAdapter的类图session
HttpRequestHandlerAdapter的源码以下List-1所示,由support的实现可知这个处理的状况是:controller类实现了接口HttpRequestHandler的状况。mvc
List-1 HttpRequestHandlerAdapter的support和handle方法源码ide
public class HttpRequestHandlerAdapter implements HandlerAdapter { @Override public boolean supports(Object handler) { return (handler instanceof HttpRequestHandler); } @Override public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { ((HttpRequestHandler) handler).handleRequest(request, response); return null; } ... }
SimpleControllerHandlerAdapter类在spring-webmvc模块中,它实现的HandlerAdapter接口是org.springframework.web.servlet.HandlerAdapter。ui
SimpleControllerHandlerAdapter的部分源码以下List-2所示,由其support方法可知,这个类处理的是咱们定义的controller类实现了org.springframework.web.servlet.mvc.Controller接口的状况。this
List-2 SimpleControllerHandlerAdapter的support及其它方法源码
@Override public boolean supports(Object handler) { return (handler instanceof Controller); } @Override public void handleAction(ActionRequest request, ActionResponse response, Object handler) throws Exception { ((Controller) handler).handleActionRequest(request, response); } @Override public ModelAndView handleRender(RenderRequest request, RenderResponse response, Object handler) throws Exception { return ((Controller) handler).handleRenderRequest(request, response); } ......
AnnotationMethodHandlerAdapter在spring-webmvc模块中,如今已经被Deprecated。
图4 AnnotationMethodHandlerAdapter的类继承图
AnnotationMethodHandlerAdapter的support方法及其它以下,一眼看上去比上面分析的俩个复杂多了。
List-3 AnnotationMethodHandlerAdapter的support方法及其它方法源码
@Override public boolean supports(Object handler) { return getMethodResolver(handler).hasHandlerMethods(); } @Override public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { Class<?> clazz = ClassUtils.getUserClass(handler); Boolean annotatedWithSessionAttributes = this.sessionAnnotatedClassesCache.get(clazz); if (annotatedWithSessionAttributes == null) { annotatedWithSessionAttributes = (AnnotationUtils.findAnnotation(clazz, SessionAttributes.class) != null); this.sessionAnnotatedClassesCache.put(clazz, annotatedWithSessionAttributes); } if (annotatedWithSessionAttributes) { checkAndPrepare(request, response, this.cacheSecondsForSessionAttributeHandlers, true); } else { checkAndPrepare(request, response, true); } // Execute invokeHandlerMethod in synchronized block if required. if (this.synchronizeOnSession) { HttpSession session = request.getSession(false); if (session != null) { Object mutex = WebUtils.getSessionMutex(session); synchronized (mutex) { return invokeHandlerMethod(request, response, handler); } } } return invokeHandlerMethod(request, response, handler); } ......
来看下AnnotationMethodHandlerAdapter的support方法时序图,原图在Github上。
图5 AnnotationMethodHandlerAdapter的support时序图