SpringMVC源码深度解析之HandlerAdapter适配器模式源码分析

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

如果不采用适配器的话

If(hanlder instanceof Controller){

 // 执行Controller适配器
}

If(hanlder instanceof  HttpControler){

 // 执行我们的HttpController

}

If(hanlder instanceof  ServletControler){

 // 执行我们的HttpController

}

If(hanlder instanceof  AnnotationControler){

 // 执行我们的AnnotationController
}

简单实现接口

@Controller("/httpRequestHandler")
public class ExtHttpRequestHandlerAdapter implements HttpRequestHandler {

    @Override
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("httpRequestHandler");
    }
}

这个时候就会执行到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); }

public void handle(Object handler) {
    ((AnnotationController) handler).hanlder();
}

}

public class HttpRequestHandlerAdapter implements HandlerAdapter { /** * Http类型 适配器 */ public boolean supports(Object handler) { return (handler instanceof HttpController); }

public void handle(Object handler) {
    ((HttpController) handler).hanlder();
}

}

Controller

public interface Controller {

**//请求方法**
void 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 handlerAdapters;

public DispatcherServlet() {
    handlerAdapters = new ArrayList<HandlerAdapter>();
    handlerAdapters.add(new HttpRequestHandlerAdapter());
    handlerAdapters.add(new AnnotationHandlerAdapter());
}
public void dispatcher() {
    **// 1. 已经获取到hanlder**
    AnnotationController hanlder = new AnnotationController();
 **// 2.获取具体适配器**
    HandlerAdapter handlerAdapter = getHandlerAdapter(hanlder);
    **// 3.执行我们的请求方案**
    handlerAdapter.handle(hanlder);
}
public HandlerAdapter getHandlerAdapter(Controller controller) {
    if (this.handlerAdapters != null) {
        for (HandlerAdapter ha : this.handlerAdapters) {

            if (ha.supports(controller)) {
                return ha;
            }
        }
    }
    return null;
}
public static void main(String\[\] args) {
    new DispatcherServlet().dispatcher();
}

}

本文参考:

蚂蚁课堂

http://www.mayikt.com/

评论

暂无评论