spring boot datasource configuration

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)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store