天天看点

聊聊Mybatis的插件接口之责任链模式

聊聊Mybatis的插件接口之责任链模式

Mybatis定义了插件接口来用于扩展拦截

拦截器接口

Interceptor接口:

public interface Interceptor {
  Object intercept(Invocation invocation) throws Throwable;

  default Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }

  default void setProperties(Properties properties) {
  }

}
           

自定义拦截器

我们可以实现Interceptor接口来做拦截

@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {
                MappedStatement.class, Object.class, RowBounds.class,
                ResultHandler.class}),
        @Signature(type = Executor.class, method = "close", args = {boolean.class})
})
public class XppPlugin implements Interceptor {
    
}
           

类上的注解@Intercepts表示这个类是拦截器类,@Signature定义需要拦截的类,方法,方法对应参数,像我们定义的这个类拦截的方法就是Executor 接口中的 query(MappedStatement, Object, RowBounds, ResultHandler) 方法和 close(boolean) 方法。

配置拦截器

定义完拦截器之后,我们还需要在全局配置文件中配置这个拦截器

<plugins>
  <plugin interceptor="org.mybatis.example.XppPlugin">
    <property name="someProperty" value="100"/>
  </plugin>
</plugins>
           

<plugin>

节点解析成Interceptor对象保存在Configuration.interceptorChain对象中

InterceptorChain是构建Interceptor责任链的类,通过遍历每个拦截器,调用lanjie器 的plugin()方法处理目标对象

public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }
           

plugin()中调用Plugin.wrap(target, this);来生成代理对象,Plugin 实现 InvocationHandler接口来作为代理类

  1. 调用getSignatureMap()方法解析@Signature注解定义的方法集合
  2. 如果当前对象在注解定义的方法集合中,就通过Proxy.newProxyInstance(type.getClassLoader(), interfaces, new Plugin(target, interceptor, signatureMap));创建代理对象
  3. invoke()方法中如果给定的方法在signatureMap集合中,就调用拦截器进行拦截处理,调用Interceptor的intercept()方法

总结