Mastering Spring Boot 2.0
上QQ阅读APP看书,第一时间看更新

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.