mybatis源码解析八之other

拦截器

public interface Interceptor {
//执行拦截逻辑的方法
  Object intercept(Invocation invocation) throws Throwable;
//决定是否触发intercept ()方法
  default Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
//根据配置初始化Interceptor 对象
  default void setProperties(Properties properties) {
    // NOP
  }

}

Configuration

public class Configuration {

  public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
   //根据参数,选择合适的Executor 实现
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
   //根据配置决定是否开启二级缓存的功能
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
   //创建Executor的代理对象
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }
  }

InterceptorChain

public class InterceptorChain {

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

  public Object pluginAll(Object target) {
  //遍历interceptors 集合
    for (Interceptor interceptor : interceptors) {
     //调用
      target = interceptor.plugin(target);
    }
    return target;
  }

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

  public List<Interceptor> getInterceptors() {
    return Collections.unmodifiableList(interceptors);
  }

}

Plugin

public class Plugin implements InvocationHandler {
  //目标对象
  private final Object target;
  //Interceptor 对象
  private final Interceptor interceptor;
  //记录了@ Signature j主解中的信息
  private final 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) {
  //负责处理@Signature注解
    Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
  //获取目标类型
    Class<?> type = target.getClass();
  //获取目标类型实现的接口
    Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
    if (interfaces.length > 0) {
    //使用JDK 动态代理的方式创建代理对象
      return Proxy.newProxyInstance(
          type.getClassLoader(),
          interfaces,
          new Plugin(target, interceptor, signatureMap));
    }
    return target;
  }

  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      //获取当前方法所在类或接口中,可被当前Interceptor 拦截的方法
      Set<Method> methods = signatureMap.get(method.getDeclaringClass());
      //如果当前调用的方法需妥被拦截,则调用interceptor.intercept()方法进行拦截处理
      if (methods != null && methods.contains(method)) {
        return interceptor.intercept(new Invocation(target, method, args));
      }
      //如果当前调用的方法不能被拦截, 9J1J 调用target 对象的相应方法
      return method.invoke(target, args);
    } catch (Exception e) {
      throw ExceptionUtil.unwrapThrowable(e);
    }
  }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值