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

当前回答

@Value("${your.elements}")    
private String[] elements;

YML 文件:

your:
 elements: element1, element2, element3

其他回答

@Value("${your.elements}")    
private String[] elements;

YML 文件:

your:
 elements: element1, element2, element3

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

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

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

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

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

@Value("#{'${your.elements}'.split(',')}")  
private Set<String> stringSet;

YML 文件:

your:
 elements: element1, element2, element3

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