Spring Boot Configure and Use Two DataSources

后端 未结 9 2080
不思量自难忘°
不思量自难忘° 2020-11-22 04:26

How can I configure and use two data sources?

For example here is what I have for the first data source:

application.properties



        
9条回答
  •  臣服心动
    2020-11-22 05:14

    Refer the official documentation


    Creating more than one data source works same as creating the first one. You might want to mark one of them as @Primary if you are using the default auto-configuration for JDBC or JPA (then that one will be picked up by any @Autowired injections).

    @Bean
    @Primary
    @ConfigurationProperties(prefix="datasource.primary")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    @ConfigurationProperties(prefix="datasource.secondary")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    

提交回复
热议问题