Using the @EnableConfigurationProperties annotation

The @EnableConfigurationProperties annotation in the configuration class specifies and auto-injects the container bean. Let's see the following configuration class file:

@Configuration 
@EnableConfigurationProperties(ConnectionSettings.class) 
public class AccountsClientConfiguration { 
   // Spring initialized this automatically 
   @Autowired  
   ConnectionSettings connectionSettings; 
 
   @Bean  
   public AccountClient accountClient() { 
         return new AccountClient( 
               connectionSettings.getHost(), 
               connectionSettings.getPort(),  
               ... 
         ); 
   } 
}

This is often unnecessary, however, because all of the configuration classes behind Spring Boot auto-configuration are already annotated with @EnableConfigurationProperties.

Get Mastering Spring Boot 2.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.