The Spring Boot documentation says that to use the @ConfigurationProperties
annotation
You also need to list the properties classes to regis
As M. Deinum referred @EnableConfigurationProperties
Is for enabling support of @ConfigurationProperties
. If you take a look to the annotation Java Doc you can see:
Enable support for ConfigurationProperties annotated beans. ConfigurationProperties beans can be registered in the standard way (for example using Bean @Bean methods) or, for convenience, can be specified directly on this annotation. [...]
For example, let's say you have a class whose responsibility is to read and store information from your application.yml
/ application.properties
that is required to make a connection to different databases. You annotate it with @ConfigurationProperties
.
Then, you typically have a @Configuration
annotated class that provides a DataSource
@Bean
to your application. You can use the @EnableConfigurationProperties
to link it to the @ConfigurationProperties
class and init your data sources accordingly.
Here is a small example:
application.yml
data-sources:
db1:
url: "jdbc:postgresql://localhost:5432}/db1"
username: test
password: test
db2:
url: "jdbc:postgresql://localhost:5432}/db2"
username: test
password: test
DataSourcesConfiguration
@ConfigurationProperties
public class DataSourcesConfiguration {
private Map dataSources;
public void setDataSources(Map dataSources) {
this.dataSources = dataSources;
}
Map getDataSources() {
return dataSources;
}
}
DataSourceConnectionConfiguration
@Configuration
@EnableConfigurationProperties(DataSourcesConfiguration.class)
public class DatabaseConnectionConfiguration implements Provider {
private DataSourcesConfiguration dataSourcesConfiguration;
public DatabaseConnectionConfiguration(DataSourcesConfiguration dataSourcesConfiguration) {
this.dataSourcesConfiguration = dataSourcesConfiguration;
}
@Bean
public DataSource dataSource() {
// Use dataSourcesConfiguration to create application data source. E.g., a AbstractRoutingDataSource..
}
}