英文:
How to set default value for Map in spring @Value?
问题
I am having below property in properties file
test.feature={UPPER:{id:'54'}}
Below is the bean I am tying to get the value from properties file.
@Bean
public Map<String, Map<String, String>> test(
@Value("#{${test.feature}}")
Map<String, Map<String, String>> ids) {
return ids;
}
It's working fine. But if we not configure the property, want to set the default value.
I tried in different ways but nothing helps.
@Value("#{${test.feature:{T(java.util.Collections).emptyMap()}}}")
getting IllegalStateException: Cannot convert value of type 'java.util.ArrayList' to required type 'java.util.Map': no matching editors or conversion strategy found.
@Value("#{${test.feature} ?: { : }}")
@Value("#{${test.feature:}}")
I have tried above ways but nothing helps.
Note: I'm using spring 5.x and not in spring boot.
How to fix this?
英文:
I am having below property in properties file
test.feature={UPPER:{id:'54'}}
Below is the bean I am tying to get the value from properties file.
@Bean
public Map<String, Map<String, String>> test(
@Value("#{${test.feature}}")
Map<String, Map<String, String>> ids) {
return ids;
}
It's working fine. But if we not configure the property, want to set the default value.
I tried in different ways but nothing helps.
@Value("#{${test.feature:{T(java.util.Collections).emptyMap()}}}")
getting IllegalStateException: Cannot convert value of type 'java.util.ArrayList' to required type 'java.util.Map': no matching editors or conversion strategy found.
@Value("#{${test.feature} ?: { : }}")
@Value("#{${test.feature:}}")
I have tried above ways but nothing helps.
Note: I'm using spring 5.x and not in spring boot.
How to fix this?
答案1
得分: 1
@Value("#{${populate.map:{'k1':'v1','k2':'v2'}}}") // 结果是 {"k1":"v1","k2":"v2"}
private Map<String, String> map3;
或者:
@Value("#{${test.feature:{k1:{nk1:'v1'}}}}") Map<String,Map<String,String>> map
否则:
@Value("#{${test.feature:{T(java.util.Collections).emptyMap()}}}") Map<String, String> map3
英文:
If the map is not empty, it can be written as follows:
@Value("#{${populate.map:{'k1':'v1','k2':'v2'}}}") // result is {"k1":"v1","k2":"v2"}
private Map<String, String> map3;
or:
@Value("#{${test.feature:{k1:{nk1:'v1'}}}}") Map<String,Map<String,String>> map
otherwise:
@Value("#{${test.feature:{T(java.util.Collections).emptyMap()}}}") Map<String, String> map3
答案2
得分: 0
-
这是如何在.properties文件中定义映射的方式:
myMap.key1 = value1
myMap.key2 = value2
myMap.key3 = value3
-
以及如何处理嵌套映射:
nestedMap.key1.nestedKey1 = nestedValue1
nestedMap.key1.nestedKey2 = nestedValue2
nestedMap.key2.nestedKey3 = nestedValue3
nestedMap.key2.nestedKey4 = nestedValue4
英文:
1. This is how you can define map in .properties file
myMap.key1 = value1
myMap.key2 = value2
myMap.key3 = value3
2. And how to deal with nested map:
nestedMap.key1.nestedKey1=nestedValue1
nestedMap.key1.nestedKey2=nestedValue2
nestedMap.key2.nestedKey3=nestedValue3
nestedMap.key2.nestedKey4=nestedValue4
答案3
得分: 0
我已经测试过,这按照你想要的方式工作:
@Value("#{${test.feature:{T(java.util.Collections).emptyMap()}}}")
Map<String, Map<String, String>> ids) {
return ids;
}
英文:
I have tested and this works as you wanted it:
@Value("#{${test.feature:{T(java.util.Collections).emptyMap()}}}")
Map<String, Map<String, String>> ids) {
return ids;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论