Spring boot 在编译时验证application.yml中的属性名称

Spring boot 在编译时验证application.yml中的属性名称,spring-boot,validation,properties,annotations,application.properties,Spring Boot,Validation,Properties,Annotations,Application.properties,我想检查是否可以在编译时而不是运行时验证属性名。 我的application.yml需要这样的数据- john: firstName: John lastName: Doe age: 30 address: efghhhhh email: john.doe@gmail.com 我的ApplicationProperties.java是这样的- @Data @Configuration @PropertySource(value = "cla

我想检查是否可以在编译时而不是运行时验证属性名。 我的application.yml需要这样的数据-

  john:
    firstName: John
    lastName: Doe
    age: 30
    address: efghhhhh
    email: john.doe@gmail.com
我的ApplicationProperties.java是这样的-

@Data
@Configuration
@PropertySource(value = "classpath:employee.yml", factory = YamlPropertySourceFactory.class)
public class ApplicationProperties {

    private Employee john;
}
private final ApplicationProperties applicationProperties;
applicationProperties.getJohn();
我的控制器得到这样的数据-

@Data
@Configuration
@PropertySource(value = "classpath:employee.yml", factory = YamlPropertySourceFactory.class)
public class ApplicationProperties {

    private Employee john;
}
private final ApplicationProperties applicationProperties;
applicationProperties.getJohn();
我想确保如果有人错误地将“john”更改为“joohn”,它应该在编译时抛出一个错误。现在编译工作正常,并且在运行时抛出NullPointerException。是否可以对属性名进行编译时检查?如果是的话,请告诉我怎么做


谢谢

不是在编译时,但是您可以通过为道具创建
@Validated@ConfigurationProperties(prefix=“john”)
并将类似
@NotNull
的验证规则置于相关属性之上,确保应用程序快速失败

有关更多信息,请参阅