繁体   English   中英

如何在 spring 引导中的 application.yml 中定义空列表

[英]How to define empty list in application.yml in spring boot

我的 spring 启动项目中有 application.yml 并想声明空列表。 我的意思是有一个两级配置层次结构。 这个 class 是获取 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,因为 value []被分配给列表。 如何正确书写?

在外部配置中应该怎么写呢? 如果我有时想声明值但有时它必须为空

name: AUTHORS_IDS
      value: 

你只需要放一个冒号。 我已经解决了我的问题,它有效。

book:
   authors: ${AUTHORS_IDS:}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM