我试图从应用程序加载字符串数组。yml文件。这是配置:

ignore:
    filenames:
        - .DS_Store
        - .hg

这是类片段:

@Value("${ignore.filenames}")
private List<String> igonoredFileNames = new ArrayList<>();

在同一个类中,还有其他配置可以正常加载。我的YAML文件中没有选项卡。尽管如此,我还是得到了以下异常:

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'ignore.filenames' in string value "${ignore.filenames}"

当前回答

yaml文件中的配置:

ignore:
    filenames: >
        .DS_Store
        .hg

弹簧组件:

@Value("#{'${gnore.filenames}'.split(' ')}")
private List<String> igonoredFileNames;

这对我来说很有效。

其他回答

在我的例子中,这是.yml文件中的语法问题。我有:

@Value("${spring.kafka.bootstrap-servers}")
public List<String> BOOTSTRAP_SERVERS_LIST;

我的。yml文件中的列表:

bootstrap-servers:
    - s1.company.com:9092
    - s2.company.com:9092
    - s3.company.com:9092

没有读入@Value-annotated字段。当我将.yml文件中的语法更改为:

bootstrap-servers: >
    s1.company.com:9092,
    s2.company.com:9092,
    s3.company.com:9092

它运行得很好。

yaml文件中的配置:

ignore:
    filenames: >
        .DS_Store
        .hg

弹簧组件:

@Value("#{'${gnore.filenames}'.split(' ')}")
private List<String> igonoredFileNames;

这对我来说很有效。

我唯一能做的就是这样

servers: >
    dev.example.com,
    another.example.com

@Value("${servers}")
private String[] array;

别忘了你类上面的@Configuration ....

没有“别离”,就没有这样的运气……

也可以(引导1.5.8版本)

servers: 
       dev.example.com,
       another.example.com
@Value("#{'${your.elements}'.split(',')}")  
private Set<String> stringSet;

YML 文件:

your:
 elements: element1, element2, element3

有很多你可以玩春季法术。

除了Ahmet的答案外,您还可以使用>符号向彗差分隔的字符串添加换行符。

application.yml:

ignoreFilenames: >
  .DS_Store, 
  .hg

Java代码:

@Value("${ignoreFilenames}")    
String[] ignoreFilenames;