12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- package com.jiayue.ipfcst.client.config;
- import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.Primary;
- import javax.sql.DataSource;
- import java.util.HashMap;
- import java.util.Map;
- @Configuration
- public class DynamicDataSourceConfig {
- @Bean
- @ConfigurationProperties("spring.datasource.datasource1")
- public DataSource firstDataSource(){
- return DruidDataSourceBuilder.create().build();
- }
- // @Bean
- // @ConfigurationProperties("spring.datasource.datasource2")
- // public DataSource secondDataSource(){
- // return DruidDataSourceBuilder.create().build();
- // }
- //
- // @Bean
- // @ConfigurationProperties("spring.datasource.datasource3")
- // public DataSource thirdDataSource(){
- // return DruidDataSourceBuilder.create().build();
- // }
- @Bean
- @Primary
- public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource,DataSource thirdDataSource) {
- Map<Object, Object> targetDataSources = new HashMap<>(5);
- targetDataSources.put(DataSourceNames.FIRST, firstDataSource);
- // targetDataSources.put(DataSourceNames.SECOND, secondDataSource);
- // targetDataSources.put(DataSourceNames.Third, thirdDataSource);
- return new DynamicDataSource(firstDataSource, targetDataSources);
- }
- }
|