The Spring Boot documentation says that to use the @ConfigurationProperties
annotation
You also need to list the properties classes to register in the
@EnableConfigurationProperties
annotation, as shown in the following example:
and gives this code:
@Configuration @EnableConfigurationProperties(AcmeProperties.class) public class MyConfiguration { }
But in the very next paragraph says:
Even if the preceding configuration creates a regular bean for AcmeProperties, we recommend that
@ConfigurationProperties
only deal with the environment and, in particular, does not inject other beans from the context. Having said that, the@EnableConfigurationProperties
annotation is also automatically applied to your project so that any existing bean annotated with@ConfigurationProperties
is configured from theEnvironment
.
Suggesting that listing a @ConfigurationProperties
bean under an @EnableConfigurationProperties
annotation is not necessary.
So which is it? Experimentally, I've seen that if I annotate a bean with @ConfigurationProperties
it gets properties injected to it as expected without needing to list it in @EnableConfigurationProperties
, but if this is the case then why list anything that has a @ConfigurationProperties
annotation under @EnableConfigurationProperties
, as is shown in the documentation? Does it make any kind of difference?
@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.
@EnableConfigurationProperties annotation is strictly connected to @ConfiguratonProperties. It enables support for @ConfigurationProperties annotated classes in our application. However, it's worth to point out that the Spring Boot documentation says, every project automatically includes @EnableConfigurationProperties.
@ConfigurationProperties allows to map the entire Properties and Yaml files into an object easily. It also allows to validate properties with JSR-303 bean validation. By default, the annotation reads from the application.
@Configuration annotation indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
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<String, BasicDataSource> dataSources; public void setDataSources(Map<String, BasicDataSource> dataSources) { this.dataSources = dataSources; } Map<String, BasicDataSource > getDataSources() { return dataSources; } }
DataSourceConnectionConfiguration
@Configuration @EnableConfigurationProperties(DataSourcesConfiguration.class) public class DatabaseConnectionConfiguration implements Provider<Connection> { 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.. } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With