Configuring multiple datasources in Spring

Let’s assume you’re using Flyway as database migration tool. If your DBA does not give out credentials that allows you to alter the schema or issuing DDL statements, you have to configure a second database that tracks the Flyway migrations. Here it is without any further explanations how to configure a second database in Spring. Both databases can be configured in the application.properties. The @FlywayDataSource annotation is provided by Spring to set the database for Flyway.


@Configuration
public class PersistenceConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="flyway.datasource")
@FlywayDataSource
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
}