在我的类(class)中,我有 2 个通过 @Value 注释预先填充的私有(private)字段。尽管可以从 .properties 文件中正确读取该值,但如果在 .properties 文件中未设置它的默认值,则永远不会应用它。
我尝试创建 PropertySourcesPlaceholderConfigurer bean 并指定文件的位置:“classpath:application.properties”。这些是我的属性:
@Value("${year:2019}")
private Integer year;
资源/application.properties:
year=
设置年份后,整数年份将收到正确的值。如果它保持为空,我希望设置默认值(2019),但它仍然为空。
Spring 版本:5.1.8.RELEASE
最佳答案
它为空,因为您的属性文件将其设置为空白。如果您从属性文件中删除year=,您应该获得默认值。
关于java - 为什么在基于注释的 Spring 应用程序中 @Value 默认值被解析为 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56954556/