【发布时间】:2021-07-20 03:08:01
【问题描述】:
我的 spring boot 项目中有 application.yml 并想声明空列表。我的意思是有一个两级配置层次结构。需要这个类来获取在 application.yml 中声明的值
@Configuration
@ConfigurationProperties("book")
public class PropertiesReader {
private List<Long> authors;
public List<Long> getAuthors() {
return new ArrayList<>(authors);
}
public void setAuthors(List<Long> authors) {
this.authors= new ArrayList<>(authors);
}
这就是 application.yml 部分的样子
book:
library: ${LIBRARY_REFERENCE:library}
secondValue:
expirationAfter: 10
authors: ${AUTHORS_IDS:[]}
因此,大括号中的值在另一个外部配置文件中声明。但是如果没有声明值,spring应该分配在冒号之后声明的默认值。在单个值中它可以工作,但是在 List 中,如果我如上所述声明它,我会得到 NumberFormatException,因为值 [] 已分配给列表。如何正确书写?
在外部配置中也应该怎么写?如果我有时想声明值,但有时它必须为空
name: AUTHORS_IDS
value:
【问题讨论】:
标签: java spring spring-boot yaml