zoukankan      html  css  js  c++  java
  • 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功能,”

    关于@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来指定自定义的资源目录。
    

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

  • 相关阅读:
    P1495 曹冲养猪
    luoguP4281[AHOI2008]紧急集合 / 聚会
    NOIP1997 代数表达式
    luogu P3709大爷的字符串题
    luoguP3912 素数个数
    POJ 1065 Wooden Sticks
    POJ 1063 Flip and Shift
    POJ 1062 昂贵的聘礼
    POJ 1003 Hangover
    POJ 1001 Exponentiation
  • 原文地址:https://www.cnblogs.com/wbyixx/p/12521797.html
Copyright © 2011-2022 走看看