黑暗的笑 2019-07-17 17:52 采纳率: 0%
浏览 510

mybatis 多个interceptor 为什么不会被覆盖?

mybatis 多个interceptor 为什么不会被覆盖?

类(org.apache.ibatis.session.Configuration.java)

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    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) interceptorChain.pluginAll(executor);
    return executor;
  }

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

看代码怎么感觉拦截器会被覆盖啊?求告知,如何实现链式的

  • 写回答

1条回答 默认 最新

  • Light Gao 优质创作者: 编程框架技术领域 2024-06-25 17:02
    关注

    MyBatis 的多个拦截器不会被覆盖,因为它们通过链式调用依次应用。pluginAll 方法遍历拦截器列表,每个拦截器处理后返回的对象会被下一个拦截器继续处理,确保所有拦截器都被应用。

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

    newExecutor 方法中,executor 通过 interceptorChain.pluginAll(executor) 处理后包含了所有拦截器的功能。

    评论

报告相同问题?