Not registered via @EnableConfigurationProperties or marked as Spring component

一、问题来源

  学习Spring Boot 时,利用@ConfigurationProperties(prefix = "")来绑定属性时报错

二、报错信息

Not registered via @EnableConfigurationProperties or marked as Spring component

三、解决过程

  用可爱的百度一搜,小白们也碰到一样的问题,回答也不少,

  https://blog.csdn.net/qq_42635345/article/details/85761314比如这位仁兄。

  我解决的时候在@ConfigurationProperties注解下,添加了@EnableConfigurationProperties就OK了,至于在pom.xml中配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-configuration-processor</artifactId>
	<optional>true</optional>
</dependency>  

  依赖来解决此问题,应该是不对题的,这段代码的作用是配置文件处理器,解决最开始粉色背景部分(Spring Boot Configuration Annotation Processor not found in classpath)的问题。所以我将这段代码注释后,并没有报错。如下图所示。

四、总结

  SpringBoot中,将类中的属性和配置文件中的配置进行绑定时出现的问题

五、延伸

  @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;

  只有这个组件是容器中的组件,容器才能提供的@ConfigurationProperties功能;所以需要配置@Component才可使用 。
  
原文地址:https://www.cnblogs.com/peanut-zh/p/13192766.html