mybatis插件

Mybatis插件相关的接口或类有:Interceptor、InterceptChain、Plugin和Invocation
Mybatis拦截的主要对象:executor, statementHandler,parameterHandler,resultHandler

Mybatis插件主要流程:
1. Configuration类中,在初始化Executor、ParameterHandler、ResultSetHandler和StatementHandler都会调用interceptorChain.pluginAll()这个函数
2. interceptorChain.pluginAll()函数可以执行所有的Interceptor.plugin()获得拦截对象的代理对象。
3. 当调用拦截对象的方法时会调用Interceptor.intercept()方法

Interceptor

源码如下:

package org.apache.ibatis.plugin;
import java.util.Properties;
public interface Interceptor {
 //在此方法中实现自己需要的功能,最后执行invocation.proceed()方法,实际就是调 用method.invoke(target, args)方法,调用代理类 
  Object intercept(Invocation invocation) throws Throwable;
  //这个方法是将target生成代理类 
  Object plugin(Object target);
  //在xml中注册Intercept是配置一些属性 
  void setProperties(Properties properties);
}

InterceptorChain:保存了所有自定义Interceptor拦截器

源码如下:

package org.apache.ibatis.plugin;

import java.util.ArrayList;
import java.util.List;

public class InterceptorChain {

  private final List<Interceptor> interceptors = new ArrayList<Interceptor>();

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

  public void addInterceptor(Interceptor interceptor) {
    interceptors.add(interceptor);
  }

}

Plugin:获得拦截对象的代理类
源码如下:

//这个类是Mybatis拦截器的核心,大家可以看到该类继承了InvocationHandler  
//又是JDK动态代理机制  
public class Plugin implements InvocationHandler {  

  //目标对象  
  private Object target;  
  //拦截器  
  private Interceptor interceptor;  
  //记录需要被拦截的类与方法  
  private Map<Class<?>, Set<Method>> signatureMap;  

  private Plugin(Object target, Interceptor interceptor, Map<Class<?>, Set<Method>> signatureMap) {  
    this.target = target;  
    this.interceptor = interceptor;  
    this.signatureMap = signatureMap;  
  }  

  //一个静态方法,对一个目标对象进行包装,生成代理类。  
  public static Object wrap(Object target, Interceptor interceptor) {  
    //首先根据interceptor上面定义的注解 获取需要拦截的信息  
    Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);  
    Class<?> type = target.getClass();  
    Class<?>[] interfaces = getAllInterfaces(type, signatureMap);  
    //如果长度为>0 则返回代理类 否则不做处理  
    if (interfaces.length > 0) {  
      //创建JDK动态代理对象  
      return Proxy.newProxyInstance(  
          type.getClassLoader(),  
          interfaces,  
          new Plugin(target, interceptor, signatureMap));  
    }  
    return target;  
  }  

  //在执行Executor、ParameterHandler、ResultSetHandler和StatementHandler的实现类的方法时会调用这个方法  
  @Override  
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
    try {  
      //通过method参数定义的类 去signatureMap当中查询需要拦截的方法集合  
      Set<Method> methods = signatureMap.get(method.getDeclaringClass());  
      //判断是否是需要拦截的方法,如果需要拦截的话就执行实现的Interceptor的intercept方法,执行完之后还是会执行method.invoke方法,不过是放到interceptor实现类中去实现了  
      if (methods != null && methods.contains(method)) {  
        return interceptor.intercept(new Invocation(target, method, args));  
      }  
      //不拦截 直接通过目标对象调用方法  
      return method.invoke(target, args);  
    } catch (Exception e) {  
      throw ExceptionUtil.unwrapThrowable(e);  
    }  
  }  
  //根据拦截器接口(Interceptor)实现类上面的注解获取相关信息  
  private static Map<Class<?>, Set<Method>> getSignatureMap(Interceptor interceptor) {  
    //获取注解信息  
    Intercepts interceptsAnnotation = interceptor.getClass().getAnnotation(Intercepts.class);  
    // issue #251  
    //为空则抛出异常  
    if (interceptsAnnotation == null) {  
      throw new PluginException("No @Intercepts annotation was found in interceptor " + interceptor.getClass().getName());        
    }  
    //获得Signature注解信息  
    Signature[] sigs = interceptsAnnotation.value();  
    Map<Class<?>, Set<Method>> signatureMap = new HashMap<Class<?>, Set<Method>>();  
    //循环注解信息  
    for (Signature sig : sigs) {  
      //根据Signature注解定义的type信息去signatureMap当中查询需要拦截方法的集合  
      Set<Method> methods = signatureMap.get(sig.type());  
      //第一次肯定为null 就创建一个并放入signatureMap  
      if (methods == null) {  
        methods = new HashSet<Method>();  
        signatureMap.put(sig.type(), methods);  
      }  
      try {  
        //找到sig.type当中定义的方法 并加入到集合  
        Method method = sig.type().getMethod(sig.method(), sig.args());  
        methods.add(method);  
      } catch (NoSuchMethodException e) {  
        throw new PluginException("Could not find method on " + sig.type() + " named " + sig.method() + ". Cause: " + e, e);  
      }  
    }  
    return signatureMap;  
  }  
  //根据对象类型与signatureMap获取接口信息  
  private static Class<?>[] getAllInterfaces(Class<?> type, Map<Class<?>, Set<Method>> signatureMap) {  
    Set<Class<?>> interfaces = new HashSet<Class<?>>();  
    //循环type类型的接口信息 如果该类型存在与signatureMap当中则加入到set当中去  
    while (type != null) {  
      for (Class<?> c : type.getInterfaces()) {  
        if (signatureMap.containsKey(c)) {  
          interfaces.add(c);  
        }  
      }  
      type = type.getSuperclass();  
    }  
    //转换为数组返回  
    return interfaces.toArray(new Class<?>[interfaces.size()]);  
  }  

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值