繁体   English   中英

Springboot @ConfigurationProperties to Map of Maps or (Nested Map/MultiKeyMap)

[英]Springboot @ConfigurationProperties to Map of Maps or (Nested Map/MultiKeyMap)

我试图通过yaml文件在域上保留一些业务配置。

yml配置文件的结构:

bank:
    accountType1:
        debit:
            - Product1
            - Product2
        hybrid:
            - ProductX
            - ProductY
    accountType2:
        debit:
            - Product1
            - Product2
            - Product3
        hybrid:
            - ProductX
            - ProductY
            - ProductZ

我在域上有以下枚举。

enum AccountType{ ACCOUNT_TYPE1, ACCOUNT_TYPE2}
enum Feature{ DEBIT, HYBRID}
enum Products { Product1, Product2, Product3, ProductX, ProductY, ProductZ }

我想寻求帮助以将这些业务配置作为 MultikeyMap 或嵌套的 map,但使用枚举作为键。 例如:

Map<AccountType, Map<Feature, List<Products>>

基本上我希望配置执行以下查找:

if AccountType = accountType1 & Feature = debit, then return [Product1,Product2]

我找不到通过配置文件优雅地执行此操作的方法,因为它总是初始化为 null

@ConfigurationProperties(prefix="bank")
public class ProductResolver {
Map<AccountType, Map<Feature, List<Products>> configMap 
  // NestedMap, Apache MultiKeymap or something similar either should be ok for me
}

有高手可以指导一下吗?

如果你可以稍微改变你的映射,就像这样:

bank:
 map:
  accountType1:
    debit:
     - Product1
     - Product2
    hybrid:
     - ProductX
     - ProductY

然后下面的配置实际上对我有用:

@ConfigurationProperties(prefix = "bank")
public class NewProps {

    private Map<AccountType, Map<String, List<Product>>> map = new HashMap<>();

    public Map<AccountType, Map<String, List<Product>>> getMap() {
        return map;
    }

    public void setMap(Map<AccountType, Map<String, List<Product>>> map) {
        this.map = map;
    }
}

暂无
暂无

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

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