- Mastering Spring Boot 2.0
- Dinesh Rajput
- 72字
- 2021-06-25 21:29:16
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.