SpringBoot在yml中對Bean進(jìn)行注入的時候,使用@ConfigurationProperties注解的時候亦歉,需要使用@Component注解
使用@ConfigurationProperties(prefix ="命名不建議包含大寫")
image.png
open Documentation... 官方文檔介紹:
You can easily generate your own configuration metadata file from items annotated with
@ConfigurationProperties
by using the
spring-boot-configuration-processor
jar. The jar includes a Java annotation processor which is invoked as your project is compiled. To use the processor, include a dependency on
spring-boot-configuration-processor
.
maven 依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
image.png
不使用@Component注解就會報(bào)出紅框內(nèi)的錯誤:Not registered via @EnableConfigurationProperties or marked as Spring component
此時需要添加@Component注解聲明將這個組件添加到容器才可以被使用
“當(dāng)這個組件是容器中的組件時,才能使用容器提供的@ConfigurationProperties功能”