springboot配置多数据源

Posted ly570

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot配置多数据源相关的知识,希望对你有一定的参考价值。

如果使用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);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
配置文件 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
1
2
3
4
5
6
7
8
9
注意事项

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

@Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者
当然,primary意味着"主要的",类似与SQL语句中的"primary key",有且只能有一个

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

[[email protected] 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配置多数据源的主要内容,如果未能解决你的问题,请参考以下文章

springboot捕获全局异常和配置多数据源

springboot-配置多个数据源

Spring Boot 配置多数据源

SpringBoot入门之基于Druid配置Mybatis多数据源

springboot之多数据源配置JdbcTemplate

基于springboot的多数据源自动配置实现