为什么在基于注释的 Spring 应用程序中 @Value 默认值被解析为 null?

Why in the annotation-based Spring app @Value default value is getting resolved to null?

在我的 class 中,我有 2 个通过 @Value 注释预填充的私有字段。尽管可以从 .properties 文件中正确读取该值,但如果在 .properties 文件中未设置它的默认值,则永远不会应用它。

我已经尝试创建 PropertySourcesPlaceholderConfigurer bean 并指定文件的位置:"classpath:application.properties"。这些是我的属性:

@Value("${year:2019}")
private Integer year;

resources/application.属性:

year=

设置年份后,整数年份会收到正确的值。如果它仍然为空,我希望设置默认值 (2019),但它仍然为空。

Spring版本:5.1.8.RELEASE

它为空,因为您的属性文件将其设置为空白。如果您从属性文件中删除 year= ,您应该获得默认值。