What difference does @EnableConfigurationproperties make if a bean is already annotated with @ConfigurationProperties?

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..
    }

}

It took me a while to reach to this post but would like to add here so that others may get benefited.

@ConfigurationProperties - Used to bind a class with an externalized property file. Very powerful and must be used to separate out bean classes with configuration entity class.

@Configuration - Creates a Spring bean of configuration stereotype.

@EnableConfigurationProperties - Creates a binding between a configuration entity class and Spring configuration stereotype so that after injection within a service properties can be retrieved easily.


If we look at the code below:

@Configuration @EnableConfigurationProperties @ConfigurationProperties(prefix="ar1") public class ar1Settings { }

  • @Configuration tells Spring to treat this as a configuration class and register it as a Bean

  • @EnableConfigurationProperties tells Spring to treat this class as a consumer of application.yml/properties values

  • @ConfigurationProperties tells Spring what section this class represents.

My understanding is that if you don't need to specify the section of the property file, then @ConfigurationProperties can be omitted.