开发者社区 问答 正文

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

 

展开
收起
爱吃鱼的程序员 2020-06-06 21:06:10 655 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>代码2(报错):</p> 
    
    @Configuration

    改为

    @Component
                        用@Value注解 
                    
    
                        <div class='ref'><h4>引用来自“JYD_XL”的评论</h4><p>代码2(报错):</p> 
    
    @Configuration

    改为

    @Component
                        <div class='ref'><h4>引用来自“赵云涛”的评论</h4>用@Value注解 
    
    2020-06-06 21:06:25
    赞同 展开评论
问答分类:
问答标签:
问答地址: