字符串列表的 Spring Boot yaml 配置 [英] Spring Boot yaml configuration for a list of strings

查看:46
本文介绍了字符串列表的 Spring Boot yaml 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 application.yml 文件加载字符串数组.这是配置:

I am trying to load an array of strings from application.yml file. This is the config:

ignore:
    filenames:
        - .DS_Store
        - .hg

这是课程:

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

同一个类中还有其他配置加载得很好.我的 YAML 文件中没有选项卡.尽管如此,我仍然收到以下异常:

There are other configurations in the same class that loads just fine. There are no tabs in my YAML file. Still, I get the following exception:

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

推荐答案

我的猜测是,@Value 无法处理复杂"类型.你可以使用这样的道具类:

My guess is, that the @Value can not cope with "complex" types. You can go with a prop class like this:

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

请注意:此代码是 Groovy - 不是 Java - 以保持示例简短!有关如何采用的提示,请参阅评论.

Please note: This code is Groovy - not Java - to keep the example short! See the comments for tips how to adopt.

查看完整示例 https://github.com/christoph-frick/so-springboot-yaml-string-list

这篇关于字符串列表的 Spring Boot yaml 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆