Have spent quite sometime to auto apply the datasource configuration from yml, turns out it could be achieved through the
@ConfigurationProperties
annotations.
So with a configuration in yaml,
spring: profiles: dev datasource: hikari: auto-commit: true connection-timeout: 30000 maximum-pool-size: 20 url: jdbc:sqlserver://.. username: password:
and a bean configuration
@Bean(name = "RODataSource") @ConfigurationProperties("spring.datasource.hikari") public DataSource getDataSource(){ HikariDataSource dataSource = DataSourceBuilder.create() .type(HikariDataSource.class) .url(url) .username(username).password(pwd) .driverClassName(driver) .build(); return dataSource; }
The configurationProperties is able to reflect what’s the Bean and apply the corresponding properties.
(auto-commit, pool size and timeout value for example)