All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.config.datasource.dynamic.support.DataSourceAutoConfiguration Maven / Gradle / Ivy

The newest version!
package com.feingto.cloud.config.datasource.dynamic.support;

import com.feingto.cloud.config.datasource.dynamic.EnableDynamicDataSource;
import com.feingto.cloud.config.datasource.dynamic.adapter.DataSourceMethodAdvice;
import com.feingto.cloud.config.datasource.dynamic.adapter.DataSourceMethodMatcherPointcutAdvisor;
import com.feingto.cloud.config.datasource.dynamic.transaction.DynamicDataSourceTransactionManager;
import com.feingto.cloud.config.datasource.dynamic.transaction.JpaDataSourceTransactionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.Advisor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.NonNull;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

/**
 * 多数据源、事务自动注入
 *
 * @author longfei
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(annotation = EnableDynamicDataSource.class)
@ConditionalOnClass(EntityManagerFactory.class)
@EnableTransactionManagement(proxyTargetClass = true)
public class DataSourceAutoConfiguration implements TransactionManagementConfigurer {
    private PlatformTransactionManager transactionManager;

    @Bean
    @ConditionalOnClass(name = "org.springframework.orm.jpa.JpaTransactionManager")
    @ConditionalOnProperty(prefix = "spring.datasource.master-slave", name = "enabled", havingValue = "false", matchIfMissing = true)
    public PlatformTransactionManager transactionManager(EntityManagerFactory factory) {
        log.info("Bean '{}' has been injected", JpaTransactionManager.class.getName());
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(factory);
        this.transactionManager = transactionManager;
        return transactionManager;
    }

    @Bean("transactionManager")
    @ConditionalOnClass(name = "org.springframework.orm.jpa.JpaTransactionManager")
    @ConditionalOnProperty(prefix = "spring.datasource.master-slave", name = "enabled", havingValue = "true")
    public PlatformTransactionManager msTransactionManager(EntityManagerFactory factory) {
        log.info("Bean '{}' has been injected", JpaDataSourceTransactionManager.class.getName());
        JpaDataSourceTransactionManager transactionManager = new JpaDataSourceTransactionManager();
        transactionManager.setEntityManagerFactory(factory);
        this.transactionManager = transactionManager;
        return transactionManager;
    }

    @Bean("mybatis")
    @ConditionalOnClass(name = {"org.apache.ibatis.session.SqlSessionFactory", "org.mybatis.spring.SqlSessionFactoryBean"})
    @ConditionalOnProperty(prefix = "spring.datasource.master-slave", name = "enabled", havingValue = "false", matchIfMissing = true)
    public PlatformTransactionManager mybatisTransactionManager(DataSource dataSource) {
        log.info("Bean '{}' has been injected", DataSourceTransactionManager.class.getName());
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean("mybatis")
    @ConditionalOnClass(name = {"org.apache.ibatis.session.SqlSessionFactory", "org.mybatis.spring.SqlSessionFactoryBean"})
    @ConditionalOnProperty(prefix = "spring.datasource.master-slave", name = "enabled", havingValue = "true")
    public PlatformTransactionManager mybatisMsTransactionManager(DataSource dataSource) {
        log.info("Bean '{}' has been injected", DynamicDataSourceTransactionManager.class.getName());
        DynamicDataSourceTransactionManager transactionManager = new DynamicDataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean
    public Advisor dataSourceAdvisor() {
        DataSourceMethodMatcherPointcutAdvisor advisor = new DataSourceMethodMatcherPointcutAdvisor();
        advisor.setAdvice(new DataSourceMethodAdvice());
        advisor.setOrder(-1);
        return advisor;
    }

    @NonNull
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return this.transactionManager;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy