SpringMVC中的HandlerAdapter适配器
什么是适配器模式
定义:将一个系统的接口转换成另外一种形式,从而使原来不能直接调用的接口变得可以调用。
适配器模式应用场景
- Mybatis多种日志框架的整合
- SpringMVC适配器模式
- 新老版本的兼容问题
SpringMVC适配器模式源码分析
1、通过URL找到具体的请求方法
mappedHandler = this.getHandler(processedRequest);

2、使用getHandlerAdapter获取对应的hanlder的具体HandlerAdapter,然后通过具体的适配器执行方法
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());


在这里进行初始化三个适配器


先走父类

返回true

protected boolean supportsInternal(HandlerMethod handlerMethod) { return true; }
拿到对应的适配器

HandlerAdapter接口看下所有适配器类型

下面看下这几种适配器:
AbstractHandlerMethodAdapter implements HandlerAdapter
public final boolean supports(Object handler) { return handler instanceof HandlerMethod && this.supportsInternal((HandlerMethod)handler); }
HttpRequestHandlerAdapter implements HandlerAdapter
public boolean supports(Object handler) { return handler instanceof HttpRequestHandler; }
RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
protected boolean supportsInternal(HandlerMethod handlerMethod) { return true; }
SimpleControllerHandlerAdapter implements HandlerAdapter
public boolean supports(Object handler) { return handler instanceof Controller; }
SimpleServletHandlerAdapter implements HandlerAdapter
public boolean supports(Object handler) { return handler instanceof Servlet; }
- 继承Controller方式所使用的适配器:SimpleControllerHandlerAdapter
- HTTP请求处理器适配器:HttpRequestHandlerAdapter
- 注解方式(@Controller)的处理器适配器:RequestMappingHandlerAdapter
如果不采用适配器的话
1If(hanlder instanceof Controller){ 2 3 // 执行Controller适配器 4} 5 6If(hanlder instanceof HttpControler){ 7 8 // 执行我们的HttpController 9 10} 11 12If(hanlder instanceof ServletControler){ 13 14 // 执行我们的HttpController 15 16} 17 18If(hanlder instanceof AnnotationControler){ 19 20 // 执行我们的AnnotationController 21} 22
简单实现接口
1@Controller("/httpRequestHandler") 2public class ExtHttpRequestHandlerAdapter implements HttpRequestHandler { 3 4 @Override 5 public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 6 System.out.println("httpRequestHandler"); 7 } 8}
这个时候就会执行到HttpRequestHandlerAdapter适配器
模拟SpringMVC适配器模式
HandlerAdapter
public interface HandlerAdapter { /** * 根据hanlder判断是那个HandlerAdapter类型 如果找到对应的类型话返回true */ boolean supports(Object handler); /** * 执行我们的请求方法 */ void handle(Object handler); }
HandlerAdapter子类
public class AnnotationHandlerAdapter implements HandlerAdapter { /** * 注解形式的适配器 */ public boolean supports(Object handler) { return (handler instanceof AnnotationController); }
1public void handle(Object handler) { 2 ((AnnotationController) handler).hanlder(); 3}
}
public class HttpRequestHandlerAdapter implements HandlerAdapter { /** * Http类型 适配器 */ public boolean supports(Object handler) { return (handler instanceof HttpController); }
1public void handle(Object handler) { 2 ((HttpController) handler).hanlder(); 3}
}
Controller
public interface Controller {
1**//请求方法** 2void hanlder();
}
Controller子类
public class AnnotationController implements Controller { public void hanlder() { System.out.println("AnnotationController"); } }
public class HttpController implements Controller { public void hanlder() { System.out.println("HttpController"); } }
DispatcherServlet
public class DispatcherServlet { private List<HandlerAdapter> handlerAdapters;
1public DispatcherServlet() { 2 handlerAdapters = new ArrayList<HandlerAdapter>(); 3 handlerAdapters.add(new HttpRequestHandlerAdapter()); 4 handlerAdapters.add(new AnnotationHandlerAdapter()); 5} 6public void dispatcher() { 7 **// 1. 已经获取到hanlder** 8 AnnotationController hanlder = new AnnotationController(); 9 **// 2.获取具体适配器** 10 HandlerAdapter handlerAdapter = getHandlerAdapter(hanlder); 11 **// 3.执行我们的请求方案** 12 handlerAdapter.handle(hanlder); 13} 14public HandlerAdapter getHandlerAdapter(Controller controller) { 15 if (this.handlerAdapters != null) { 16 for (HandlerAdapter ha : this.handlerAdapters) { 17 18 if (ha.supports(controller)) { 19 return ha; 20 } 21 } 22 } 23 return null; 24} 25public static void main(String\[\] args) { 26 new DispatcherServlet().dispatcher(); 27}
}
本文参考:
蚂蚁课堂
