侧边栏壁纸
博主头像
qingtian博主等级

喜欢是一件细水流长的事,是永不疲惫的双向奔赴~!

  • 累计撰写 85 篇文章
  • 累计创建 40 个标签
  • 累计收到 0 条评论

如何在Spring中配置多个数据源

qingtian
2021-01-23 / 0 评论 / 0 点赞 / 1,169 阅读 / 4,012 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2021-01-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

如何在Spring中配置多个数据源

在SpringBoot中通过结合SpringBoot的自动配置功能,我们可以在使用中配置多个数据源,以便在实际使用中选择。

代码示例

创建一个SpringBoot工程,所需依赖如下:

在本案例中为了简便,我们直接使用内置的H2 DataBase。

2021-01-23_180940

除去相关依赖

在SpringBoot中除去三个自动配置:

  1. ataSourceAutoConfiguration.class,
  2. DataSourceTransactionManagerAutoConfiguration.class,
  3. JdbcTemplateAutoConfiguration.class
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        JdbcTemplateAutoConfiguration.class})
@Slf4j
public class SpringJdbcApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringJdbcApplication.class, args);
    }


    @Bean
    @ConfigurationProperties("foo.datasource")
    public DataSourceProperties fooDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    @Primary
    public DataSource fooDataSource() {
        DataSourceProperties dataSourceProperties = fooDataSourceProperties();
        log.warn("foo datasource: {}", dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager fooTxManager(DataSource fooDataSource) {
        return new DataSourceTransactionManager(fooDataSource);
    }

    @Bean
    @ConfigurationProperties("bar.datasource")
    public DataSourceProperties barDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean
    public DataSource barDataSource() {
        DataSourceProperties dataSourceProperties = barDataSourceProperties();
        log.warn("bar datasource: {}", dataSourceProperties.getUrl());
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean
    @Resource
    public PlatformTransactionManager barTxManager(DataSource barDataSource) {
        return new DataSourceTransactionManager(barDataSource);
    }
}

  • 上面这段代码演示了如何配置两个数据源:fooDataSourcebarDataSource.

  • 配置了datasource和相应的DataSourceTransactionManager事务管理器

  • ps:通过@Primary注解告知Spring选取fooDataSource为首选datasource

    • 根据有说法不需要加上这个注解,但是根据我自己的测试(springboot2.4.2)发现报错如下:

      Description:
      
      Method h2Console in org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration required a single bean, but 2 were found:
      	- fooDataSource: defined by method 'fooDataSource' in com.qingtianblog.jdbc.springjdbc.SpringJdbcApplication
      	- barDataSource: defined by method 'barDataSource' in com.qingtianblog.jdbc.springjdbc.SpringJdbcApplication
      Action:
      Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
      
      

properties文件

management.endpoints.web.exposure.include=*
spring.output.ansi.enabled=always

foo.datasource.url=jdbc:h2:mem:foo
foo.datasource.username=sa
foo.datasource.password=

bar.datasource.url=jdbc:h2:mem:bar
bar.datasource.username=sa
bar.datasource.password=

server.port=8081

启动SpringJdbcApplication

查看控制台

2021-01-23 20:06:57.760  WARN 21404 --- [  restartedMain] c.q.j.springjdbc.SpringJdbcApplication   : foo datasource: jdbc:h2:mem:foo
2021-01-23 20:06:57.784  INFO 21404 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-01-23 20:06:57.901  INFO 21404 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-01-23 20:06:57.906  INFO 21404 --- [  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:foo'
2021-01-23 20:06:58.007  WARN 21404 --- [  restartedMain] c.q.j.springjdbc.SpringJdbcApplication   : bar datasource: jdbc:h2:mem:bar
  • 可以看到控制台中打印出了相应的foo datasource: jdbc:h2:mem:foobar datasource: jdbc:h2:mem:bar
0

评论区