springboot 读取yml报错

sucanber 发布于 2018/06/08 11:42
阅读 737
收藏 1

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

 

加载中
0
JYD_XL
JYD_XL

代码2(报错):

@Configuration

改为

@Component
0
赵云涛
赵云涛
用@Value注解
0
sucanber
sucanber

引用来自“JYD_XL”的评论

代码2(报错):

@Configuration

改为

@Component

试过了,不行

0
sucanber
sucanber

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

用@Value注解

@value 那还不如不用

@ConfigurationProperties
返回顶部
顶部