简体   繁体   English

是否可以使用具有 Spring 引导配置属性的 YAML 序列?

[英]Is it possible to user a YAML sequence with Spring Boot configuration properties?

I have a requirement for a Spring Boot application to load some configuration that would ideally be suited to a sort of mini-spreadsheet.我需要一个 Spring 引导应用程序来加载一些非常适合某种小型电子表格的配置。 Almost all the configuration consists of YAML properties files that are bound to @ConfigurationProperties POJOs thus:几乎所有配置都包含绑定到@ConfigurationProperties POJO 的 YAML 属性文件,因此:

static class Entry {
    private final Terrain terrain;    // A simple enumeration
    private final int value;
    // ... other fields

    public Entry(Terrain terrain, int value, ...) { ... }
}

@ConfigurationProperties("section")
@ConstructorBinding
@Validated
class SectionProperties {
    private final List<Entry> entries;

    public SectionProperties(List<Entry> entries) {
        this.entries=  List.copyOf(entries);
    }

    // ... other fields, getters, etc
}

This works fine if the YAML is structured as a sequence of objects like this:如果 YAML 被构造为如下对象序列,则此方法可以正常工作:

section:
  entries:
    - terrain: plains
      value: 1
    - terrain: forest
      value: 2

However it would great from the perspective of the user/author of this configuration if the data was organised as a 'spreadsheet' as they would then be able to see the various values in context of each other, rather than the big list above (there are around a dozen or so entries).但是,如果将数据组织为“电子表格”,那么从该配置的用户/作者的角度来看,这会很棒,因为他们将能够在彼此的上下文中看到各种值,而不是上面的大列表(有大约有十几个条目)。 Something like this:像这样的东西:

section:
  entries:
    - [plains, 1]
    - [forest, 2]

This is valid YAML (as far as I can tell) but the configuration binding fails dismally with the 'unbound properties' error for every entry in the list, ie it seems to recognise entries as a YAML sequence but cannot bind the data within the YAML sequence block (the square brackets) to an Entry .这是有效的 YAML (据我所知),但配置绑定失败,列表中每个条目的“未绑定属性”错误,即它似乎将entries识别为 YAML 序列,但无法绑定 Z9463F87BBED1FCDACFB8D40E185CA 中的数据序列块(方括号)到Entry

Has anyone attempted to do this or anything similar?有没有人尝试过这样做或类似的事情? Is it possible?可能吗?

Apologies if this has already been addressed elsewhere, I've searched SO and the Spring documentation but cannot find anything that covers this specific case and whether its actually feasible.抱歉,如果这已经在其他地方得到解决,我已经搜索了 SO 和 Spring 文档,但找不到任何涵盖此特定案例及其是否实际可行的内容。

This should be possible if you implement the mapping yourself:如果您自己实现映射,这应该是可能的:

@Component
@ConfigurationPropertiesBinding
public class EntryConverter implements Converter<List<String>, Entry> {
    @Autowired
    private ConversionService conversionService;

    @Override
    public Entry convert(List<String> from) {
        String[] data = from.split(",");
        return new Entry(data[0],
                conversionService.convert(data[1], Integer.class), ...);
    }
}

I am unsure whether that would work non-scalar values nested in the list an I am also unsure whether it works for List<Entry> .我不确定这是否适用于嵌套在列表中的非标量值,我也不确定它是否适用于List<Entry> Give it a try and report back:).试一试并报告:)。

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

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