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

SpringMVC中的HandlerAdapter适配器

什么是适配器模式

定义:将一个系统的接口转换成另一种形式,从而使原来不能直接调用的接口变得能够调用。app

适配器模式应用场景

  • Mybatis多种日志框架的整合
  • SpringMVC适配器模式
  • 新老版本的兼容问题

SpringMVC适配器模式源码分析

一、经过URL找到具体的请求方法框架

mappedHandler = this.getHandler(processedRequest);

二、使用getHandlerAdapter获取对应的hanlder的具体HandlerAdapter,而后经过具体的适配器执行方法ide

HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());

在这里进行初始化三个适配器源码分析

先走父类this

返回truespa

protected boolean supportsInternal(HandlerMethod handlerMethod) {
    return true;
}

拿到对应的适配器日志

HandlerAdapter接口看下全部适配器类型code

下面看下这几种适配器:blog

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<HandlerAdapter> 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/

相关文章
相关标签/搜索