Spring Boot @ConfigurationProperties 到地图的地图或(嵌套 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<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;
}
}
我正在尝试通过 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<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;
}
}