美文网首页Java程序员Java 杂谈
springboot配置多数据源(JdbcTemplate方式)

springboot配置多数据源(JdbcTemplate方式)

作者: 88b61f4ab233 | 来源:发表于2019-06-05 14:23 被阅读3次

    在实际开发中可能会遇到需要配置多个数据源的情况,比如:需要使用多个host、需要使用多种数据库(MySql、Oracle、SqlServer...)

    如果使用springboot开发,可做如下配置:

    Config:

    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import javax.sql.DataSource;
    
    @Configuration
    public class DataSourceConfig {
    
        @Bean(name = "testDataSource")
        @Primary
        @Qualifier("testDataSource")
        @ConfigurationProperties(prefix="spring.datasource.hikari.mysql")
        public DataSource testDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "formalDataSource")
        @Qualifier("formalDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.formal.mysql")
        public DataSource formalDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name="testJdbcTemplate")
        public JdbcTemplate testJdbcTemplate (
                @Qualifier("testDataSource")  DataSource testDataSource ) {
            return new JdbcTemplate(testDataSource);
        }
    
        @Bean(name = "formalJdbcTemplate")
        public JdbcTemplate formalJdbcTemplate(
                @Qualifier("formalDataSource") DataSource formalDataSource){
            return new JdbcTemplate(formalDataSource);
        }
    }
    
    

    配置文件 application.properties

    spring.datasource.hikari.mysql.jdbc-url =jdbc:mysql://mysql2.cdqdops.org:3306/standard?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
    spring.datasource.hikari.mysql.username = root
    spring.datasource.hikari.mysql.password = 123456
    spring.datasource.hikari.mysql.driver-class-Name = com.mysql.jdbc.Driver
    
    spring.datasource.formal.mysql.jdbc-url =jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
    spring.datasource.formal.mysql.username = root
    spring.datasource.formal.mysql.password = 1314
    spring.datasource.formal.mysql.driver-class-Name = com.mysql.jdbc.Driver
    

    注意事项

    使用多个数据源时,需要添加@Primary注解

    @Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者

    当然,primary意味着"主要的",类似与SQL语句中的"primary key",有且只能有一个

    开发时,别人将注释掉的代码恢复,出现了多个"@Primary",就导致了如下错误:

    [root@app4 logs]# tail stdout.log 
    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: more than one 'primary' bean found among candidates: [mysqlDataSource, formalDataSource, sqlServerDataSource]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.determinePrimaryCandidate(DefaultListableBeanFactory.java:1381)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.determineAutowireCandidate(DefaultListableBeanFactory.java:1341)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1110)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getIfUnique(DefaultListableBeanFactory.java:1728)
        at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.getDataSourceInitializer(DataSourceInitializerInvoker.java:100)
        at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:62)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
        ... 32 common frames omitted
    

    相关文章

      网友评论

        本文标题:springboot配置多数据源(JdbcTemplate方式)

        本文链接:https://www.haomeiwen.com/subject/sdajxctx.html