Spring配置绑定失败

时间:2019-05-03 10:51:25

标签: spring spring-boot spring-config

在application.yaml文件中有以下块:

foo:
  bar: bazz

我想使用@ConfigurationProperties将配置映射到配置类。

@Validated
@Getter @Setter
@ConfigurationProperties(prefix = "foo")
public class FooProperties {
    @NotNull
    private String bar;
}

这是配置类

@Configuration
@EnableConfigurationProperties(FooProperties.class)
public class FooConfiguration {
    @Bean
    public Foo getFoo(FooProperties properties) {
        ///
    }
}

但是,当我尝试启动应用程序时,出现以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'foo' to a.b.c.FooProperties failed:

    Property: foo.bar
    Value: null
    Reason: must not be null


Action:

Update your application's configuration

我还想念其他吗?我无法弄清楚为什么这种琐碎的事情会失败。

1 个答案:

答案 0 :(得分:0)

尝试将值放在单引号中,例如:

foo:
  bar: 'bazz'

由于要读取的值已映射到字符串变量。