温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HandlerAdapter适配器模式的源码分析

发布时间:2021-10-20 17:52:37 来源:亿速云 阅读:142 作者:柒染 栏目:大数据

本篇文章为大家展示了HandlerAdapter适配器模式的源码分析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

SpringMVC中的HandlerAdapter适配器

什么是适配器模式

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

适配器模式应用场景

  • Mybatis多种日志框架的整合

  • SpringMVC适配器模式

  • 新老版本的兼容问题

SpringMVC适配器模式源码分析

1、通过URL找到具体的请求方法

mappedHandler = this.getHandler(processedRequest);

HandlerAdapter适配器模式的源码分析

HandlerAdapter适配器模式的源码分析

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

HandlerAdapter适配器模式的源码分析

HandlerAdapter适配器模式的源码分析

先走父类

HandlerAdapter适配器模式的源码分析

返回true

HandlerAdapter适配器模式的源码分析

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

拿到对应的适配器

HandlerAdapter适配器模式的源码分析

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

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<HandlerAdapter> handlerAdapters;

    public DispatcherServlet() {
        handlerAdapters = new ArrayList<HandlerAdapter>();
        handlerAdapters.add(new HttpRequestHandlerAdapter());
        handlerAdapters.add(new AnnotationHandlerAdapter());
    }
    public void dispatcher() {        // 1. 已经获取到hanlderAnnotationController 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();
    }
}

上述内容就是HandlerAdapter适配器模式的源码分析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI