• Spring Boot多数据源的配置


    在实际的项目开发过程中,随着项目业务量的不断扩大,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源

    手工配置两组DataSource及相关内容

    与Spring Boot协同工作

    • 配置@Primary类型的Bean
    • 排除Spring Boot的自动配置
      • DataSourceAutoConfiguration
      • DataSourceTransactionManagerAutoConfiguration
      • JdbcTemplateAutoConfiguration

    pom.xml引入相关依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.james.demo</groupId>
        <artifactId>multi-datasource-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>multi-datasource-demo</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jdbc</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    

    application.properties分别配置两组数据源,不同数据源的配置要分开

    spring.output.ansi.enabled=always
    
    ## foo datasource
    foo.datasource.url=jdbc:h2:mem:foo
    foo.datasource.username=sa
    foo.datasource.password=
    
    ## bar datasource
    bar.datasource.url=jdbc:h2:mem:bar
    bar.datasource.username=sa
    bar.datasource.password=
    

    具体代码实现

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
    import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Primary;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;
    
    import javax.annotation.Resource;
    import javax.sql.DataSource;
    
    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,
            DataSourceTransactionManagerAutoConfiguration.class, JdbcTemplateAutoConfiguration.class})
    public class MultiDatasourceDemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MultiDatasourceDemoApplication.class, args);
        }
    
        @Bean
        public DataSource fooDataSource(){
            DataSourceProperties dataSourceProperties = fooDataSourceProperties();
            System.out.println("fooDataSource: {} "+dataSourceProperties.getUrl());
            return dataSourceProperties.initializeDataSourceBuilder().build();
        }
    
        @Bean
        @ConfigurationProperties("foo.datasource")
        public DataSourceProperties fooDataSourceProperties() {
           return new DataSourceProperties();
        }
    
        @Bean
        @Resource
        public PlatformTransactionManager fooTxManager(DataSource fooDataSource){
            return new DataSourceTransactionManager(fooDataSource);
        }
    
    
        @Bean
        @Primary
        public DataSource barDataSource(){
            DataSourceProperties dataSourceProperties = barDataSourceProperties();
            System.out.println("barDataSource: {} "+dataSourceProperties.getUrl());
            return dataSourceProperties.initializeDataSourceBuilder().build();
        }
    
        @Bean
        @ConfigurationProperties("bar.datasource")
        public DataSourceProperties barDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean
        @Resource
        public PlatformTransactionManager barTxManager(DataSource barDataSource){
            return new DataSourceTransactionManager(barDataSource);
        }
    
    
    

    运行结果如下所示

    image-20200902145010070

    关注我的技术公众号,每天都有优质技术文章推送。
    微信扫一扫下方二维码即可关注:
    在这里插入图片描述

    尚未佩妥剑,转眼便江湖,愿历尽千帆,归来仍少年 
  • 相关阅读:
    红蓝对抗之Windows内网渗透(转)
    缓存注解@Cacheable、@CacheEvict、@CachePut使用及注解失效时间
    队列数组的实现
    栈结构数组的实现
    【别贪心】Website
    【别贪心】keep-web
    vue中使用$.once(‘hook:beforeDestory’,() => {})清理定时器
    【别贪心】static_vue_ele_blog
    【别贪心】vue-news
    【别贪心】cartDemo
  • 原文地址:https://www.cnblogs.com/James-1024/p/13603298.html
Copyright © 2020-2023  润新知