先说作用:
@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的类生效。
详解:
要想 @ConfigurationProperties 注解的类让生效, 有两种方式
第一种
如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。说白了 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。
第二种
不使用 @EnableConfigurationProperties 进行注册,使用 @Component 注册
PS:
spring boot版本2.2.0.M5起 @ConfigurationProperties与@Component不能同时存在,否则会出现2次注入,也就是说,必须配合@EnableConfigurationProperties 使用 待验证…