我试图从应用程序加载字符串数组。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}"

当前回答

我唯一能做的就是这样

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

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

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

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

也可以(引导1.5.8版本)

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

其他回答

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

application.yml:

ignoreFilenames: >
  .DS_Store, 
  .hg

Java代码:

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

我的猜测是,@Value不能处理“复杂”类型。你可以像这样去上道具课:

@Component
@ConfigurationProperties('ignore')
class IgnoreSettings {
    List<String> filenames
}

请注意:这段代码是Groovy(而不是Java),以保持示例简短!有关如何收养的建议,请参阅评论。

参见完整的示例https://github.com/christoph-frick/so-springboot-yaml-string-list

在我的例子中,这是.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

它运行得很好。

我唯一能做的就是这样

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

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

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

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

也可以(引导1.5.8版本)

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

在application.yml中使用逗号分隔值

ignoreFilenames: .DS_Store, .hg

用于访问的Java代码

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

它正在工作;)