Not registered via @EnableConfigurationProperties or marked as Spring component
SpringBoot中,将类中的属性和配置文件中的配置进行绑定时出现以下的问题:
当使用@ConfigurationProperties时IDEA顶部出现这样的提示:

按照提示点击跳转到官方文档,接着在pom.xml中添加如下的配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
添加完后的效果是,当你写配置文件(yml,properties配置文件)时会有相关的提示
上面弄完后@ConfigurationProperties下面还有报错,按照提示可以看到
Not registered via @EnableConfigurationProperties or marked as Spring component,网上找到的博客有说要添加 @EnableConfigurationProperties(Person.class)【此时的Person是自定义的bean】,

添加后错误确实是没了,但是在SpringBoot的单元测试时会看到如下的错误:Could not autowire. No beans of 'Person' type found
回到自定义的bean Person中,添加注解@Component,声明将这个组件添加至容器中,这样才可以被使用?
“只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能,”

通过@ConfigurationProperties获取配置文件的相关配置属性注入bean中

浙公网安备 33010602011771号