简体   繁体   English

如何在Spring Boot配置中将yml映射绑定到Java映射?

[英]How to bind a yml map to a Java map in spring-boot configuration?

I am trying to follow this guide here: 我正在尝试遵循此指南:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Configuration-Binding https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Configuration-Binding

but I am struggling to get it to work. 但我正在努力使其正常工作。

I want to initialize a HashMap from a map defined in application.yml. 我想从application.yml中定义的地图初始化HashMap。

This is my last try at the yml-map definition: 这是我最后一次尝试yml-map定义:

symbols:
    symbolPairs.CombinationsAlpha="CombinationsAlpha"
    symbolPairs.[CombinationsAlpha]=aaabbb, bbbaaa, ccceee, dddggg
    symbolPairs.Combinations="CombinationsInteger"
    symbolPairs.[CombinationsAlpha]=000111, 222666, 999000, 151515

And this is my java class: 这是我的java类:

@Data
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "symbols")
public class SymbolsConfig {
  private Map<String, List<String>> symbolPairs = new HashMap<>();
}

I want to have "CombinationsAplha" and "CombinationsInteger" injected as keys and the values as a List of Strings. 我想将"CombinationsAplha""CombinationsInteger"作为键注入,并将值作为“字符串列表"CombinationsInteger"注入。 I am struggling to define the yml correctly. 我正在努力正确定义yml。

The @Data annotation from projectlombok generates getters and setters. 来自projectlombok@Data注释会生成getter和setter。

Your yml structure isn't correct. 您的yml结构不正确。 Change your yml like this 像这样改变你的yml

symbols:
    symbolPairs.[CombinationsAlpha]: aaabbb, bbbaaa, ccceee, dddggg
    symbolPairs.[CombinationsInteger]: 000111, 222666, 999000, 151515

Here is the output 这是输出

在此处输入图片说明

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

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