从零开始手写 mybatis(二)mybatis interceptor 插件机制详解

前景回顾

第一节 从零开始手写 mybatis(一)MVP 版本 中咱们实现了一个最基本的能够运行的 mybatis。html

常言道,万事开头难,而后中间难。java

mybatis 的插件机制是 mybatis 除却动态代理以外的第二大灵魂。git

下面咱们一块儿来体验一下这有趣的灵魂带来的痛苦与快乐~github

插件的做用

在实际开发过程当中,咱们常用的Mybaits插件就是分页插件了,经过分页插件咱们能够在不用写count语句和limit的状况下就能够获取分页后的数据,给咱们开发带来很大sql

的便利。除了分页,插件使用场景主要还有更新数据库的通用字段,分库分表,加解密等的处理。数据库

这篇博客主要讲Mybatis插件原理,下一篇博客会设计一个Mybatis插件实现的功能就是每当新增数据的时候不用数据库自增ID而是经过该插件生成雪花ID,做为每条数据的主键。设计模式

image

JDK动态代理+责任链设计模式

Mybatis的插件其实就是个拦截器功能。它利用JDK动态代理和责任链设计模式的综合运用。采用责任链模式,经过动态代理组织多个拦截器,经过这些拦截器你能够作一些你想作的事。mybatis

因此在讲Mybatis拦截器以前咱们先说说JDK动态代理+责任链设计模式。app

JDK 动态代理案例

package com.github.houbb.mybatis.plugin;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkDynamicProxy {

    /**
     * 一个接口
     */
    public interface HelloService{
        void sayHello();
    }

    /**
     * 目标类实现接口
     */
    static class HelloServiceImpl implements HelloService{

        @Override
        public void sayHello() {
            System.out.println("sayHello......");
        }

    }

    /**
     * 自定义代理类须要实现InvocationHandler接口
     */
    static  class HelloInvocationHandler implements InvocationHandler {

        private Object target;

        public HelloInvocationHandler(Object target){
            this.target = target;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("------插入前置通知代码-------------");
            //执行相应的目标方法
            Object rs = method.invoke(target,args);
            System.out.println("------插入后置处理代码-------------");
            return rs;
        }

        public static Object wrap(Object target) {
            return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                    target.getClass().getInterfaces(),new HelloInvocationHandler(target));
        }
    }

    public static void main(String[] args)  {
        HelloService proxyService = (HelloService) HelloInvocationHandler.wrap(new HelloServiceImpl());
        proxyService.sayHello();
    }

}
  • 输出
------插入前置通知代码-------------
sayHello......
------插入后置处理代码-------------

优化1:面向对象

上面代理的功能是实现了,可是有个很明显的缺陷,就是 HelloInvocationHandler 是动态代理类,也能够理解成是个工具类,咱们不可能会把业务代码写到写到到invoke方法里,框架

不符合面向对象的思想,能够抽象一下处理。

定义接口

能够设计一个Interceptor接口,须要作什么拦截处理实现接口就好了。

public interface Interceptor {

    /**
     * 具体拦截处理
     */
    void intercept();

}

实现接口

public class LogInterceptor implements Interceptor{

    @Override
    public void intercept() {
        System.out.println("------插入前置通知代码-------------");
    }

}

public class TransactionInterceptor implements Interceptor{

    @Override
    public void intercept() {
        System.out.println("------插入后置处理代码-------------");
    }

}

实现代理

public class InterfaceProxy implements InvocationHandler {

    private Object target;

    private List<Interceptor> interceptorList = new ArrayList<>();

    public InterfaceProxy(Object target, List<Interceptor> interceptorList) {
        this.target = target;
        this.interceptorList = interceptorList;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //处理多个拦截器
        for (Interceptor interceptor : interceptorList) {
            interceptor.intercept();
        }
        return method.invoke(target, args);
    }

    public static Object wrap(Object target, List<Interceptor> interceptorList) {
        InterfaceProxy targetProxy = new InterfaceProxy(target, interceptorList);
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), targetProxy);
    }

    

}

测试验证

public static void main(String[] args) {
    List<Interceptor> interceptorList = new ArrayList<>();
    interceptorList.add(new LogInterceptor());
    interceptorList.add(new TransactionInterceptor());

    HelloService target = new HelloServiceImpl();
    HelloService targetProxy = (HelloService) InterfaceProxy.wrap(target, interceptorList);
    targetProxy.sayHello();
}
  • 日志
------插入前置通知代码-------------
------插入后置处理代码-------------
sayHello......

这里有一个很明显的问题,全部的拦截都在方法执行前被处理了。

优化 2:灵活指定先后

上面的动态代理确实能够把代理类中的业务逻辑抽离出来,可是咱们注意到,只有前置代理,没法作到先后代理,因此还须要在优化下。

因此须要作更一步的抽象,

把拦截对象信息进行封装,做为拦截器拦截方法的参数,把拦截目标对象真正的执行方法放到Interceptor中完成,这样就能够实现先后拦截,而且还能对拦截对象的参数等作修改。

实现思路

代理类上下文

设计一个 Invocation 对象。

public class Invocation {

    /**
     * 目标对象
     */
    private Object target;
    /**
     * 执行的方法
     */
    private Method method;
    /**
     * 方法的参数
     */
    private Object[] args;

    public Invocation(Object target, Method method, Object[] args) {
        this.target = target;
        this.method = method;
        this.args = args;
    }

    /**
     * 执行目标对象的方法
     */
    public Object process() throws Exception{
        return method.invoke(target,args);
    }

    // 省略 Getter/Setter

}

调整接口

  • Interceptor.java
public interface Interceptor {

    /**
     * 具体拦截处理
     */
    Object intercept(Invocation invocation) throws Exception;

}
  • 日志实现
public class MyLogInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Exception {
        System.out.println("------插入前置通知代码-------------");
        Object result = invocation.process();
        System.out.println("------插入后置处理代码-------------");
        return result;
    }

}

从新实现代理类

public class MyInvocationHandler implements InvocationHandler {

    private Object target;

    private Interceptor interceptor;

    public MyInvocationHandler(Object target, Interceptor interceptor) {
        this.target = target;
        this.interceptor = interceptor;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Invocation invocation = new Invocation(target, method, args);
        // 返回的依然是代理类的结果
        return interceptor.intercept(invocation);
    }

    public static Object wrap(Object target, Interceptor interceptor) {
        MyInvocationHandler targetProxy = new MyInvocationHandler(target, interceptor);
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                targetProxy);
    }

}

最核心的就在于构建了 invocation,而后执行对应的方法。

测试

  • 代码
public static void main(String[] args) {
    HelloService target = new HelloServiceImpl();
    Interceptor interceptor = new MyLogInterceptor();
    HelloService targetProxy = (HelloService) MyInvocationHandler.wrap(target, interceptor);
    targetProxy.sayHello();
}
  • 日志
------插入前置通知代码-------------
sayHello......
------插入后置处理代码-------------

优化 3:划清界限

上面这样就能实现先后拦截,而且拦截器能获取拦截对象信息。

可是测试代码的这样调用看着很别扭,对应目标类来讲,只须要了解对他插入了什么拦截就好。

再修改一下,在拦截器增长一个插入目标类的方法。

实现

接口调整

public interface Interceptor {

    /**
     * 具体拦截处理
     *
     * @return 方法执行的结果
     * @since 0.0.2
     */
    Object intercept(Invocation invocation) throws Exception;

    /**
     * 插入目标类
     *
     * @return 代理
     * @since 0.0.2
     */
    Object plugin(Object target);

}

实现调整

能够理解为把静态方法调整为对象方法。

public class MyLogInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Exception {
        System.out.println("------插入前置通知代码-------------");
        Object result = invocation.process();
        System.out.println("------插入后置处理代码-------------");
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return MyInvocationHandler.wrap(target, this);
    }

}

测试

  • 代码
public static void main(String[] args) {
    HelloService target = new HelloServiceImpl();
    Interceptor interceptor = new MyLogInterceptor();
    HelloService targetProxy = (HelloService) interceptor.plugin(target);
    targetProxy.sayHello();
}
  • 日志
------插入前置通知代码-------------
sayHello......
------插入后置处理代码-------------

责任链模式

多个拦截器如何处理?

测试代码

public static void main(String[] args) {
    HelloService target = new HelloServiceImpl();
    //1. 拦截器1
    Interceptor interceptor = new MyLogInterceptor();
    target = (HelloService) interceptor.plugin(target);
    //2. 拦截器 2
    Interceptor interceptor2 = new MyTransactionInterceptor();
    target = (HelloService) interceptor2.plugin(target);
    // 调用
    target.sayHello();
}

其中 MyTransactionInterceptor 实现以下:

public class MyTransactionInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Exception {
        System.out.println("------tx start-------------");
        Object result = invocation.process();
        System.out.println("------tx end-------------");
        return result;
    }

    @Override
    public Object plugin(Object target) {
        return MyInvocationHandler.wrap(target, this);
    }

}

日志以下:

------tx start-------------
------插入前置通知代码-------------
sayHello......
------插入后置处理代码-------------
------tx end-------------

固然不少小伙伴看到这里其实已经想到使用责任链模式,下面咱们一块儿来看一下责任链模式。

责任链模式

责任链模式

public class InterceptorChain {

    private List<Interceptor> interceptorList = new ArrayList<>();

    /**
     * 插入全部拦截器
     */
    public Object pluginAll(Object target) {
        for (Interceptor interceptor : interceptorList) {
            target = interceptor.plugin(target);
        }
        return target;
    }

    public void addInterceptor(Interceptor interceptor) {
        interceptorList.add(interceptor);
    }
    /**
     * 返回一个不可修改集合,只能经过addInterceptor方法添加
     * 这样控制权就在本身手里
     */
    public List<Interceptor> getInterceptorList() {
        return Collections.unmodifiableList(interceptorList);
    }
}

测试

public static void main(String[] args) {
    HelloService target = new HelloServiceImpl();

    Interceptor interceptor = new MyLogInterceptor();
    Interceptor interceptor2 = new MyTransactionInterceptor();
    InterceptorChain chain = new InterceptorChain();
    chain.addInterceptor(interceptor);
    chain.addInterceptor(interceptor2);

    target = (HelloService) chain.pluginAll(target);
    // 调用
    target.sayHello();
}
  • 日志
------tx start-------------
------插入前置通知代码-------------
sayHello......
------插入后置处理代码-------------
------tx end-------------

我的的思考

拦截器是否能够改进?

实际上我的感受这里能够换一种角度,好比定义拦截器接口时,改成:

这样能够代码中能够不用写执行的部分,实现起来更加简单,也不会忘记。

public interface Interceptor {

    /**
     * 具体拦截处理
     */
    void before(Invocation invacation);

    /**
     * 具体拦截处理
     */
    void after(Invocation invacation);

}

不过这样也有一个缺点,那就是对于 process 执行的部分不可见,丧失了一部分灵活性。

抽象实现

对于 plugin() 这个方法,实际上实现很是固定。

应该对于接口不可见,直接放在 chain 中统一处理便可。

手写 mybatis 引入插件

说了这么多,若是你理解以后,那么接下来的插件实现部分就是小菜一碟。

只是将上面的思想作一个简单的实现而已。

快速体验

config.xml

引入插件,其余部分省略。

<plugins>
    <plugin interceptor="com.github.houbb.mybatis.plugin.SimpleLogInterceptor"/>
</plugins>

SimpleLogInterceptor.java

咱们就是简单的输出一下入参和出参。

public class SimpleLogInterceptor implements Interceptor{
    @Override
    public void before(Invocation invocation) {
        System.out.println("----param: " + Arrays.toString(invocation.getArgs()));
    }

    @Override
    public void after(Invocation invocation, Object result) {
        System.out.println("----result: " + result);
    }

}

执行测试方法

输出日志以下。

----param: [com.github.houbb.mybatis.config.impl.XmlConfig@3b76982e, MapperMethod{type='select', sql='select * from user where id = ?', methodName='selectById', resultType=class com.github.houbb.mybatis.domain.User, paramType=class java.lang.Long}, [Ljava.lang.Object;@67011281]
----result: User{id=1, name='luna', password='123456'}
User{id=1, name='luna', password='123456'}

是否是灰常的简单,那么是怎么实现的呢?

核心实现

接口定义

public interface Interceptor {

    /**
     * 前置拦截
     * @param invocation 上下文
     * @since 0.0.2
     */
    void before(Invocation invocation);

    /**
     * 后置拦截
     * @param invocation 上下文
     * @param result 执行结果
     * @since 0.0.2
     */
    void after(Invocation invocation, Object result);

}

启动插件

在 openSession() 的时候,咱们启动插件:

public SqlSession openSession() {
    Executor executor = new SimpleExecutor();
    //1. 插件
    InterceptorChain interceptorChain = new InterceptorChain();
    List<Interceptor> interceptors = config.getInterceptorList();
    interceptorChain.add(interceptors);
    executor = (Executor) interceptorChain.pluginAll(executor);

    //2. 建立
    return new DefaultSqlSession(config, executor);
}

这里咱们就看到了一个责任链,实现以下。

责任链

public class InterceptorChain {

    /**
     * 拦截器列表
     * @since 0.0.2
     */
    private final List<Interceptor> interceptorList = new ArrayList<>();

    /**
     * 添加拦截器
     * @param interceptor 拦截器
     * @return this
     * @since 0.0.2
     */
    public synchronized InterceptorChain add(Interceptor interceptor) {
        interceptorList.add(interceptor);

        return this;
    }

    /**
     * 添加拦截器
     * @param interceptorList 拦截器列表
     * @return this
     * @since 0.0.2
     */
    public synchronized InterceptorChain add(List<Interceptor> interceptorList) {
        for(Interceptor interceptor : interceptorList) {
            this.add(interceptor);
        }

        return this;
    }

    /**
     * 代理全部
     * @param target 目标类
     * @return 结果
     * @since 0.0.2
     */
    public Object pluginAll(Object target) {
        for(Interceptor interceptor : interceptorList) {
            target = DefaultInvocationHandler.proxy(target, interceptor);
        }

        return target;
    }

}

其中的 DefaultInvocationHandler 实现以下:

/**
 * 默认的代理实现
 * @since 0.0.2
 */
public class DefaultInvocationHandler implements InvocationHandler {

    /**
     * 代理类
     * @since 0.0.2
     */
    private final Object target;

    /**
     * 拦截器
     * @since 0.0.2
     */
    private final Interceptor interceptor;

    public DefaultInvocationHandler(Object target, Interceptor interceptor) {
        this.target = target;
        this.interceptor = interceptor;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Invocation invocation = new Invocation(target, method, args);

        interceptor.before(invocation);

        // invoke
        Object result = method.invoke(target, args);

        interceptor.after(invocation, result);

        return result;
    }

    /**
     * 构建代理
     * @param target 目标对象
     * @param interceptor 拦截器
     * @return 代理
     * @since 0.0.2
     */
    public static Object proxy(Object target, Interceptor interceptor) {
        DefaultInvocationHandler targetProxy = new DefaultInvocationHandler(target, interceptor);
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                targetProxy);
    }

}

小结

本节的实现并不难,难在要理解 mybatis 总体对于插件的设计理念,技术层面仍是动态代理,结合了责任链的设计模式。

这种套路学会以后,其实不少相似的框架,咱们本身在实现的时候均可以借鉴这种思想。

拓展阅读

从零开始手写 mybatis(一)MVP 版本

image

参考资料

Mybatis框架(8)---Mybatis插件原理(代理+责任链)

相关文章
相关标签/搜索