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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章