content: package com.gupaoedu.springcloud.seata.repoprovider.config;
  
  import com.alibaba.cloud.seata.SeataProperties;
  import com.alibaba.druid.pool.DruidDataSource;
  import io.seata.rm.datasource.DataSourceProxy;
  import io.seata.spring.annotation.GlobalTransactionScanner;
  import org.apache.commons.lang3.StringUtils;
  import org.apache.ibatis.session.SqlSessionFactory;
  import org.mybatis.spring.SqlSessionFactoryBean;
  import org.mybatis.spring.transaction.SpringManagedTransactionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
  import org.springframework.boot.context.properties.EnableConfigurationProperties;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  
  /**
   * 咕泡学院，只为更好的你
   * 咕泡学院-Mic: 2227324689
   * http://www.gupaoedu.com
   **/
  @Configuration
  @EnableConfigurationProperties({SeataProperties.class})
  public class SeataAutoConfig {
  
      @Autowired
      private DataSourceProperties dataSourceProperties;
      private ApplicationContext applicationContext;
      private SeataProperties seataProperties;
      public SeataAutoConfig(SeataProperties seataProperties, ApplicationContext applicationContext){
          this.applicationContext=applicationContext;
          this.seataProperties=seataProperties;
      }
      @Bean
      public DruidDataSource druidDataSource(){
          DruidDataSource druidDataSource = new DruidDataSource();
          druidDataSource.setUrl(dataSourceProperties.getUrl());
          druidDataSource.setUsername(dataSourceProperties.getUsername());
          druidDataSource.setPassword(dataSourceProperties.getPassword());
          druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
          return druidDataSource;
      }
  
      @Bean
      public DataSourceProxy dataSourceProxy(DruidDataSource druidDataSource){
          return new DataSourceProxy(druidDataSource);
      }
  
      @Bean
      public DataSourceTransactionManager transactionManager(DataSourceProxy dataSourceProxy) {
          return new DataSourceTransactionManager(dataSourceProxy);
      }
  
      @Bean
      public SqlSessionFactory sqlSessionFactory(DataSourceProxy dataSourceProxy) throws Exception {
          SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
          factoryBean.setDataSource(dataSourceProxy);
          factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                  .getResources("classpath*:com/gupaoedu/springcloud/seata/repoprovider/dal/mappers/*Mapper.xml"));
          factoryBean.setTransactionFactory(new SpringManagedTransactionFactory());
          return factoryBean.getObject();
      }
  
      @Bean
      public GlobalTransactionScanner globalTransactionScanner(){
          String applicationName = this.applicationContext.getEnvironment().getProperty("spring.application.name");
          String txServiceGroup = this.seataProperties.getTxServiceGroup();
          if (StringUtils.isEmpty(txServiceGroup)) {
              txServiceGroup = applicationName + "-seata-service-group";
              this.seataProperties.setTxServiceGroup(txServiceGroup);
          }
          return new GlobalTransactionScanner(applicationName, txServiceGroup);
      }
  }
  