How to specify the primary data source when configuring multiple data sources in Spring Boot?
You can specify the primary data source by using the property spring.datasource.primary in the configuration file. For example:
spring.datasource.primary=primaryDataSource
spring.datasource.secondary=secondaryDataSource
Next, in the data source configuration class, use the @Primary annotation to specify the primary data source. For example:
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
In this way, the primaryDataSource will be designated as the main data source.