mybatis的相同拦截器—切面执行的顺序

结论:拦截同一个方法的拦截器和我们在mybatis-config.xml文件中的顺序相反

1. plugin生效的两种方式

推荐阅读——plugin生效的方式

源码位置:mybatis的自动加载:org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration#sqlSessionFactory

public class MybatisAutoConfiguration {

  private static Log log = LogFactory.getLog(MybatisAutoConfiguration.class);
  //读取Spring容器中的所有的plugin插件
  @Autowired(required = false)
  private Interceptor[] interceptors;
  @Bean
  @ConditionalOnMissingBean
  public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
    factory.setDataSource(dataSource);
    factory.setVfs(SpringBootVFS.class);
    if (StringUtils.hasText(this.properties.getConfigLocation())) {
      //读取mybatis-config.xml的地址
      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    factory.setConfiguration(properties.getConfiguration());
    //放入插件
    if (!ObjectUtils.isEmpty(this.interceptors)) {
      factory.setPlugins(this.interceptors);
    }
    if (this.databaseIdProvider != null) {
      factory.setDatabaseIdProvider(this.databaseIdProvider);
    }
    if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
      factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
    }
    if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
      factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
    }
    if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
      factory.setMapperLocations(this.properties.resolveMapperLocations());
    }

    return factory.getObject();
  }
}

注意:SqlSessionFactoryBean实现了InitializingBean接口,在afterPropertiesSet()方法中将执行org.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory方法去读取ConfigLocation的xml文件,解析plugin并放入集合中。

结论:spring容器的plugin先放入到集合中,后续将mybatis-config.xml的plugin按先后顺序放入到集合中。

2. plugin切面执行的顺序

源码位置:org.apache.ibatis.plugin.InterceptorChain该方法会对target代理,并且对代理类在进行代理。一层一层的增强target类,故越靠后的Interceptor越先执行。

public class InterceptorChain {

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

  public Object pluginAll(Object target) {
    //将项目启动时注册的interceptors拿出来,执行plugin方法。
    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方法:

@Slf4j
@Intercepts(
        @Signature(type = Executor.class, method = "query", args = {
                MappedStatement.class, Object.class, RowBounds.class,
                ResultHandler.class }))
public class A1Interceptor implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        log.info("A1 before...");
        Object proceed = invocation.proceed();
        log.info("A1 after...");
        return proceed;
    }

    /**
     * 将插件对象加入到拦截器链中
     * @param target
     * @return
     */
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

实际上会调用Plugin.wrap(target, this)方法。

  public static Object wrap(Object target, Interceptor interceptor) {
    Map<Class<?>, Set<Method>> signatureMap = getSignatureMap(interceptor);
    Class<?> type = target.getClass();
    Class<?>[] interfaces = getAllInterfaces(type, signatureMap);
    if (interfaces.length > 0) {
      return Proxy.newProxyInstance(
          type.getClassLoader(),
          interfaces,
          new Plugin(target, interceptor, signatureMap));
    }
    return target;
  }

wrap方法会创建代理对象。

代理对象.png

wrapper()方法对target对象一层一层的代理。即before切面执行的顺序与放入plugins的顺序相反。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值