簡體   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