spring mvc(5) HandlerAdapter
前面我们讲到了通过HandlerMapping可以获得不同类型的处理器,可以是Controller、HttpRequestHandler、Servlet、HandlerMethod甚至是我们自定义的处理器。针对不同的处理器,需要调用不同的执行流程。HandlerAdapter的作用相当于将这个执行流程与DispatcherServlet进行了解耦。
DispatcherServlet在初始化是会加载所有容器中的HandlerAdapter,通过order属性进行排序。然后在对处理器调用时循环找到可以支持该处理器的HandlerAdapter,然后执行调用。如果容器中没有HandlerAdapter,会读取DispatcherServlet.properties配置,默认配置HttpRequestHandlerAdapter、SimpleControllerHandlerAdapter、AnnotationMethodHandlerAdapter。
DispatcherServlet初始化HandlerAdapter。
1 private void initHandlerAdapters(ApplicationContext context) { 2 this.handlerAdapters = null; 3 4 if (this.detectAllHandlerAdapters) { 5 // Find all HandlerAdapters in the ApplicationContext, including ancestor contexts. 6 Map<String, HandlerAdapter> matchingBeans = 7 BeanFactoryUtils.beansOfTypeIncludingAncestors(context, HandlerAdapter.class, true, false); 8 if (!matchingBeans.isEmpty()) { 9 this.handlerAdapters = new ArrayList<>(matchingBeans.values()); 10 // We keep HandlerAdapters in sorted order. 11 AnnotationAwareOrderComparator.sort(this.handlerAdapters); 12 } 13 } 14 else { 15 //如果detectAllHandlerAdapters为false,则使用名为handlerAdapter的HandlerAdapter 16 try { 17 HandlerAdapter ha = context.getBean(HANDLER_ADAPTER_BEAN_NAME, HandlerAdapter.class); 18 this.handlerAdapters = Collections.singletonList(ha); 19 } 20 catch (NoSuchBeanDefinitionException ex) { 21 // Ignore, we'll add a default HandlerAdapter later. 22 } 23 } 24 25 if (this.handlerAdapters == null) { 26 this.handlerAdapters = getDefaultStrategies(context, HandlerAdapter.class); 27 if (logger.isDebugEnabled()) { 28 logger.debug("No HandlerAdapters found in servlet '" + getServletName() + "': using default"); 29 } 30 } 31 }
spring mvc目前有一下的HandlerAdapter:
SimpleControllerHandlerAdapter执行Controller的调用。
1 public class SimpleControllerHandlerAdapter implements HandlerAdapter { 2 3 @Override 4 public boolean supports(Object handler) { 5 return (handler instanceof Controller); 6 } 7 8 @Override 9 @Nullable 10 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) 11 throws Exception { 12 13 return ((Controller) handler).handleRequest(request, response); 14 } 15 16 @Override 17 public long getLastModified(HttpServletRequest request, Object handler) { 18 if (handler instanceof LastModified) { 19 return ((LastModified) handler).getLastModified(request); 20 } 21 return -1L; 22 } 23 24 }
HttpRequestHandlerAdapter执行HttpRequestHandler调用。
1 public class HttpRequestHandlerAdapter implements HandlerAdapter { 2 3 @Override 4 public boolean supports(Object handler) { 5 return (handler instanceof HttpRequestHandler); 6 } 7 8 @Override 9 @Nullable 10 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) 11 throws Exception { 12 13 ((HttpRequestHandler) handler).handleRequest(request, response); 14 return null; 15 } 16 17 @Override 18 public long getLastModified(HttpServletRequest request, Object handler) { 19 if (handler instanceof LastModified) { 20 return ((LastModified) handler).getLastModified(request); 21 } 22 return -1L; 23 } 24 25 }
SimpleServletHandlerAdapter执行Servlet的调用。
1 public class SimpleServletHandlerAdapter implements HandlerAdapter { 2 3 @Override 4 public boolean supports(Object handler) { 5 return (handler instanceof Servlet); 6 } 7 8 @Override 9 @Nullable 10 public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) 11 throws Exception { 12 13 ((Servlet) handler).service(request, response); 14 return null; 15 } 16 17 @Override 18 public long getLastModified(HttpServletRequest request, Object handler) { 19 return -1; 20 } 21 22 }