开发者社区 问答 正文

springboot 读取yml报错:报错

springboot 读取yml配置,当使用prefix时就报错。

代码1(正常):

@Configuration
@ConfigurationProperties
public class AccountConfig {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name= name;
    }
}

application.yml配置
name: z张三

代码2(报错):

@Configuration
@ConfigurationProperties(prefix = "test")
public class AccountConfig {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name= name;
    }
}

application.yml配置:
test:
  name: z张三

错误信息:

Failed to bind properties under 'test' to com.test.config.AppConfig$$EnhancerBySpringCGLIB$$96509be8:

    Property: test
    Value: 
    Origin: "test" from property source "class path resource
    Reason: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.boot.context.properties.ConfigurationProperties com.test.config.AppConfig$$EnhancerBySpringCGLIB$$96509be8]

Action:

Update your application's configuration

 

展开
收起
kun坤 2020-06-07 16:36:20 656 分享 版权
1 条回答
写回答
取消 提交回答
  • 代码2(报错):

    @Configuration

    改为

    @Component
    ######用@Value注解######

    引用来自“JYD_XL”的评论

    代码2(报错):

    @Configuration

    改为

    @Component

    试过了,不行

    ######

    引用来自“赵云涛”的评论

    用@Value注解

    @value 那还不如不用

    @ConfigurationProperties
    2020-06-07 16:36:25
    赞同 展开评论