【发布时间】:2021-03-06 12:47:22
【问题描述】:
我无法将 null 设置为属性值。
这是在 YAML 文件中定义值的方式:
my-property: null
这就是我在代码中注入它的方式:
@Value("${my-property}")
private String myProperty;
出于某种原因,Spring 不断注入一个空字符串 ("") 而不是 null。我错过了什么还是这是 Spring 中的错误?
【问题讨论】:
-
这仅仅是因为 YAML 文件中的 'null' 被认为是 'null' 字符串,而不是 null 值。要获得您想要的内容,只需完全删除值,然后在 EL 中使用 null 作为默认值:@Value("${my-property:null}")
标签: java spring spring-boot yaml