@EnableConfigurationProperties注解的作用是:使使用 @ConfigurationProperties 注解的類生效蒿赢。
說明:
如果一個(gè)配置類只配置@ConfigurationProperties注解辩蛋,而沒有使用@Component瓜浸,那么在IOC容器中是獲取不到properties 配置文件轉(zhuǎn)化的bean没咙。說白了 @EnableConfigurationProperties 相當(dāng)于把使用? @ConfigurationProperties 的類進(jìn)行了一次注入。
測試發(fā)現(xiàn) @ConfigurationProperties 與 @EnableConfigurationProperties 關(guān)系特別大本谜。
測試證明:
@ConfigurationProperties與@EnableConfigurationProperties的關(guān)系。
@EnableConfigurationProperties文檔中解釋:
當(dāng)@EnableConfigurationProperties注解應(yīng)用到你的@Configuration時(shí)缸逃, 任何被@ConfigurationProperties注解的beans將自動(dòng)被Environment屬性配置。 這種風(fēng)格的配置特別適合與SpringApplication的外部YAML配置進(jìn)行配合使用厂抽。
測試發(fā)現(xiàn):
1.使用@EnableConfigurationProperties進(jìn)行注冊
一切正常需频,但是 HelloServiceAutoConfiguration 頭部不使用@EnableConfigurationProperties,測訪問報(bào)錯(cuò)筷凤。
2.不使用@EnableConfigurationProperties進(jìn)行注冊昭殉,使用@Component注冊
Controller 不變,一切正常藐守,如果注釋掉 @Component 測啟動(dòng)報(bào)錯(cuò)挪丢。
由此證明,兩種方式都是將被? @ConfigurationProperties 修飾的類卢厂,加載到 Spring Env 中乾蓬。