繁体   English   中英

如何在 Spring Boot 应用程序中动态注入和使用多个数据源?

[英]How can I inject and use multiple datasources dynamically in Spring boot application?

我有这些配置文件,uat-nyc,uat-ldn。 uat-nyc 数据源是 oracle,uat-ldn 是 mysql 服务器 这个配置在 application-uat-nyc.yml 和 application-uat-ldn.yml 中设置

我有以下配置类

    @Profile({"uat-nyc", "uat-ldn"})
    @Configuration
    @EnableConfigurationPropeties(DatSourceProperties.class)
    public class DataSourceConfig{

    private DataSourceProperties properties; // server, username, password are set here
    

    DataSource getDataSource(){// gets datasource based on profiles}
    
    }

如果我的应用程序使用 spring.profiles.active: uat-nyc,uat-ldn 运行,它会创建两个数据源吗?

一个来自 uat-nyc 的配置,另一个来自 uat-ldn

我在下面有一个函数,在这个函数中,我从第三方服务获取数据,并且根据 ldn 或 nyc ,我需要坚持到 ldn 或 nyc 数据库中。 如果其他部分是动态的,我怎样才能使下面的内容成为动态? 如何在下面 getProducts 方法的 if else 部分中获取各自的数据源,即 ldn 和 nyc?

 class Product{
       String name;
       int price;
       int region;
    }
    
     @Component
     Class ProductLoader{

        JdbcTemplate jdbcTemplate;
        
         public ProductLoader(DataSource ds){
            
                 jdbcTemplate = new JdbcTemplate(ds);
         }
    
        public void getProducts(){
             List<Product> products = // rest service to get products
             if(Product product : product){
                          if(product.getRegion().equals("LONDON"){
                            //write to LONDON datbase
                           // How can I get ldn datasource here?
                          }
                          if else(product.getRegion().equals("NewYork"){
                               //write to NewYork datbase
                               How can I get NewYork datasource here? 
                          }
                          else{
                               // Unknown location
                          }
             }

 
    }
}

问题 -

  1. 如果我的应用程序使用 spring.profiles.active: uat-nyc,uat-ldn 运行,它会创建两个数据源吗?
  2. 如何将数据源动态注入 ProductLoader 并为 ldn 和 nyc 使用特定数据源

第一次你需要告诉 spring 你将使用两个数据源将由上下文 spring 管理。 在@Configuration 类上使用@Bean,然后使用@Autowired 来声明由spring 管理的变量。

你可以使用@Qualifier 来选择和限定你的豆子

@Configuration
public class ConfigDataSource {


    // example for dataSource
    
    @Bean("dataSourceWithPropOnCode") // this name will qualify on @autowired
    public DataSource dataSourceWithPropOnCode() {
        return DataSourceBuilder.create().url("").password("").username("").driverClassName("").build();
    }

    @Bean("dataSourceWithPropFromProperties")  // this name will qualify on @autowired
    @ConfigurationProperties(prefix="spring.datasource.yourname-datasource") // this is the name for the prefix for datasource on .properties settings
    public DataSource dataSourcePostgres() {
        return DataSourceBuilder.create().build();
    }

    // example for jdbctemplate
    
    @Bean("jdbcTemaplateWithPropFromProperties")  // this name will qualify on @autowired
    public JdbcTemplate jdbcTemplatePostgres(@Qualifier("dataSourceWithPropFromProperties") DataSource dataSource) {
     return new JdbcTemplate(dataSource);
    }
    
    @Bean("jdbcTemaplateWithPropOnCode")  // this name will qualify on @autowired
    public JdbcTemplate jdbcTemplatePostgres(@Qualifier("dataSourceWithPropOnCode") DataSource dataSource) {
     return new JdbcTemplate(dataSource);
    }
    
    
}

属性上的设置


spring.datasource.yourname-datasource.url=...
spring.datasource.yourname-datasource.jdbcUrl=${spring.datasource.yourname-datasource}
spring.datasource.yourname-datasource.username=user
spring.datasource.yourname-datasource.password=pass
spring.datasource.yourname-datasource.driver-class-name=your.driver


在服务上使用


    @Qualifier("jdbcTemaplateWithPropFromProperties")
    @Autowired
    private JdbcTemplate jdbcTemplate1;
    
    @Qualifier("jdbcTemaplateWithPropOnCode")
    @Autowired
    private JdbcTemplate jdbcTemplate2;
    
    @Qualifier("dataSourceWithPropOnCode")
    @Autowired
    private DataSource dataSource1;
    
    private DataSource dataSource2;
    
    public someContructorIfYouPrefer(@Qualifier("dataSourceWithPropFromProperties") @Autowired private DataSource dataSource2){
        this.dataSource2 = dataSource2;
    }
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM