For data sources, @ConfigurationProperties binds the spring.datasource.* style keys in your configuration to a DataSource or its properties object. That keeps the URL, credentials and connection pool settings out of the code and lets you define more than one data source cleanly.
In a single-database Spring Boot application you do not need to write anything: auto-configuration reads spring.datasource.* and builds a pooled DataSource (HikariCP by default). You use @ConfigurationProperties yourself when you define several data sources or customise the pool.
Boot binds spring.datasource.url, username, password and driver-class-name automatically, and pool settings under spring.datasource.hikari.*.
Reading the password from an environment variable keeps secrets out of the repository.
Place @ConfigurationProperties on a @Bean method to bind properties directly to the returned object's setters. This is the classic pattern for multiple databases.
HikariCP expects jdbc-url, not url, when you bind straight to a HikariDataSource. This difference is a common cause of the error that the url property is not set.
A more flexible form binds to DataSourceProperties, then builds the pool from it. That works with any pool and keeps the standard property names.
Here app.reports.datasource.url uses the normal url key, while pool tuning goes under the hikari sub-prefix.
| Situation | Approach |
|---|---|
| One database | Rely on auto-configuration and spring.datasource.* |
| Two or more databases | One @Bean per DataSource, with distinct prefixes, one marked @Primary |
Need standard url key with custom pool | Bind DataSourceProperties first |
| Typed settings of your own | A @ConfigurationProperties record outside the data source |
When you define your own DataSource bean, Boot's auto-configured one backs off.
You bound url to a HikariDataSource. Use jdbc-url, or bind through DataSourceProperties.
Yes, for the one that injection points and auto-configured components such as JPA should use by default. Otherwise Spring reports multiple candidates.
Avoid it. Use environment variables, a secrets manager or Spring Cloud Config with encryption.
Yes. Use @Validated on a properties class, or rely on the pool to fail fast at startup when the database is unreachable.
@ConfigurationProperties for data sources externalises connection and pool settings from code. Use auto-configuration for a single database, and one prefixed @Bean per database when you need several, remembering jdbc-url for Hikari and @Primary for the default.