简体   繁体   中英

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

I'm trying to retain a few business configs on the domain through yaml file.

Structure of yml config file:

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

I have the below enums for on the domain.

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

I would like to seek help to get these business configs as a MultikeyMap, or a nested map, but using enums as keys. For eg:

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

Basically I would like the config to perform the following lookup:

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

I couldn't find a way to do this elegantly via config files, as it always initialises to 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
}

Any experts, please could you guide me?

If you can slightly change you mappings, like this:

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

Then below config actually worked for me just fine:

@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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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