码迷,mamicode.com
首页 > 编程语言 > 详细

Not registered via @EnableConfigurationProperties or marked as Spring component

时间:2019-05-11 13:25:24      阅读:388      评论:0      收藏:0      [点我收藏+]

标签:使用   方式   style   tin   目录结构   end   marked   结构   ref   

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功能,”

技术图片

 

 

关于@Component和@EnableConfigurationProperties找到一个讨论,觉得应该有用,只是现在还不是很理解

https://www.imooc.com/qadetail/299025

技术图片

 

 

确保添加了依赖

<dependency>

   <groupId> org.springframework.boot </groupId>

   <artifactId> spring-boot-configuration-processor </artifactId>

   <optional> true </optional>

</dependency>

如果发现@ConfigurationPropertie不生效,有可能是项目的目录结构问题,可以通过@EnableConfigurationProperties(ConnectionSettings.class)来明确指定需要用哪个实体类来装载配置信息。

Spring boot 1.5以上去除了location属性,可采用@Component的方式注册为组件,然后使用@PropertySource来指定自定义的资源目录。

 

Not registered via @EnableConfigurationProperties or marked as Spring component

标签:使用   方式   style   tin   目录结构   end   marked   结构   ref   

原文地址:https://www.cnblogs.com/Guhongying/p/10848251.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!