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

问题描述 投票:0回答:1

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

我尝试创建 PropertySourcesPlaceholderConfigurer bean 并指定文件的位置:“classpath:application.properties”。这些是我的财产:

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

资源/application.properties:

year=

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

春季版本:5.1.8.RELEASE

java spring properties
1个回答
3
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.