How do I separate configuration setup for datasources when running with Spring Boot?

The  DatabaseStarter project and specifically the DatabaseAutoConfiguration class establishes the default connection parameters. For each of the DataSources, it calls buildDataSource() which uses the default configuration properties - database.user, database.url, etc.

Here is a snippet of code from the buildDataSource() method along with how the webEventDS is setup:

    @ConditionalOnMissingBean(name={"webEventDS"})
    @Bean
    public DataSource webEventDS() throws ClassNotFoundException {
        return buildDataSource();
    }

    ....

    protected DataSource buildDataSource() throws ClassNotFoundException {
        DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(props.getUrl());
        org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) DataSourceBuilder
                .create()
                .username(props.getUser())
                .password(props.getPassword())
                .url(props.getUrl())
                .driverClassName(driver.getDriverClassName())
                .type(org.apache.tomcat.jdbc.pool.DataSource.class)
                .build();
        
        String validationQuery = driver.getValidationQuery();
        if (validationQuery != null) {
            ds.setTestOnBorrow(true);
            ds.setValidationQuery(validationQuery);
        }
        
        return ds;
    }

For each of the default DataSources (eg. webDS, webEventDS, etc) you can override the Bean to build the DataSource using configuration parameters of your choosing. In the CorePersistenceConfig class (this is just a basic @Configuration bean in your core project) the default Autowired beans can be overridden to include what is needed for the given datasource. For example, here is an overridden webEventDS Bean that uses different configuration parameters. With this example in place, configuration setting like "event.database.username, event.database.password, etc) will be used.

Note too that this example uses the Spring Boot Configuration "prefix" to reference properties in a property file.  The properties prefixed with "event.database.*" will be bound to the jdbc configuration.  As part of the binding, additional connection pool properties can be used.


@Configuration
public class CorePersistenceConfig {

    @Autowired
    @Qualifier("webDS")
    DataSource webDS;

    @Autowired
    @Qualifier("webSecureDS")
    DataSource webSecureDS;

    @Autowired
    @Qualifier("webStorageDS")
    DataSource webStorageDS;

    @Autowired
    @Qualifier("webEventDS")
    DataSource webEventDS;

    @Bean
    @ConfigurationProperties(prefix="event.database")
    public DataSource webEventDS(Environment env) throws ClassNotFoundException {
        org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) DataSourceBuilder        
                .create()
                .type(org.apache.tomcat.jdbc.pool.DataSource.class)
                .build();
        return ds;
    } 

    ...
}

Then, the following is an example of setting the properties for "event.database"

event.database.username=event_user
event.database.password=event_password
event.database.url=jdbc:mysql://127.0.0.1:3306/event_database?useUnicode=true&characterEncoding=utf8
event.database.driver-class-name=com.mysql.jdbc.Driver
event.database.initial-size=10
event.database.min-idle=5
event.database.max-active=20
event.database.max-idle=20