SpringBoot在yml中对Bean进行注入的时候,使用@ConfigurationProperties注解的时候,需要使用@Component注解
使用@ConfigurationProperties(prefix ="命名不建议包含大写")
open Documentation... 官方文档介绍:
Generating Your Own Metadata by Using the Annotation Processor
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>
不使用@Component注解就会报出红框内的错误:Not registered via @EnableConfigurationProperties or marked as Spring component
此时需要添加@Component注解声明将这个组件添加到容器才可以被使用
“当这个组件是容器中的组件时,才能使用容器提供的@ConfigurationProperties功能”