Spring Boot 如何给SqlSessionFactoryBean 加插件

背景:项目中需要对数据库的写操作进行拦截。使用Mybatis的plugins可以完整这个操作

之前使用Spring+mybatis时只需要在xml中SqlSessionFactoryBean中进行配置。其中配置文件信息和插件代码

<property name="plugins">
  <array>
      <bean class="com.meituan.service.movie.emember.web.DataSourceStopWritePlugin"/>
  array>
property>
// ExamplePlugin.java
@Intercepts({@Signature(
  type= Executor.class,
  method = "update",
  args = {MappedStatement.class,Object.class})})
public class ExamplePlugin implements Interceptor {
  public Object intercept(Invocation invocation) throws Throwable {
    return invocation.proceed();
  }
  public Object plugin(Object target) {
    return Plugin.wrap(target, this);
  }
  public void setProperties(Properties properties) {
  }
}

而在使用SpringBoot时,由于SqlSessionFactoryBean是在MybatisAutoConfiguration中初始化SqlSessionFactory时配置的,而不是作为bean进行注入。所以我们的代码中没有办法拿到SqlSessionFactoryBean实例对其设置plugins。在查阅了相关用法得知我们只需要将上面的ExamplePlugin实例交给Spring管理就可以自动设置到SqlSessionFactoryBean中。因此只需要在ExamplePlugin类的注解上加上@component注解即可。

背后原理:
查看MybatisAutoConfiguration关于配置sqlSessionFactory的源码

@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())) {
      factory.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
    }
    Configuration configuration = this.properties.getConfiguration();
    if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
      configuration = new Configuration();
    }
    if (configuration != null && !CollectionUtils.isEmpty(this.configurationCustomizers)) {
      for (ConfigurationCustomizer customizer : this.configurationCustomizers) {
        customizer.customize(configuration);
      }
    }
    factory.setConfiguration(configuration);
    if (this.properties.getConfigurationProperties() != null) {
      factory.setConfigurationProperties(this.properties.getConfigurationProperties());
    }
    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();
  }

代码中

if (!ObjectUtils.isEmpty(this.interceptors)) {
    factory.setPlugins(this.interceptors);
} 

是对插件的设置。这里的interceptors是在配置类初始化时通过interceptorsProvider获取。

public MybatisAutoConfiguration(MybatisProperties properties,
                                 ObjectProvider<Interceptor[]> interceptorsProvider,
                                 ResourceLoader resourceLoader,
                                 ObjectProvider<DatabaseIdProvider> databaseIdProvider,
                                 ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
   this.properties = properties;
   this.interceptors = interceptorsProvider.getIfAvailable();
   this.resourceLoader = resourceLoader;
   this.databaseIdProvider = databaseIdProvider.getIfAvailable();
   this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
 }

interceptorsProvider是ObjectProvider类型,interceptorsProvider.getIfAvailable()的意思就是获取beanFactory中所有的Interceptor实例。
关于ObjectProvider的解读可参考下面两篇文章:
https://spring.io/blog/2016/03/04/core-container-refinements-in-spring-framework-4-3
https://blog.csdn.net/alex_xfboy/article/details/83342164

你可能感兴趣的:(mybatis)