如何在Spring的@Value注解中为Map设置默认值?

huangapple go评论90阅读模式
英文:

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:&#39;54&#39;}}

Below is the bean I am tying to get the value from properties file.

    @Bean
    public Map&lt;String, Map&lt;String, String&gt;&gt; test(
        @Value(&quot;#{${test.feature}}&quot;)
        Map&lt;String, Map&lt;String, String&gt;&gt; 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(&quot;#{${test.feature:{T(java.util.Collections).emptyMap()}}}&quot;)
getting IllegalStateException: Cannot convert value of type &#39;java.util.ArrayList&#39; to required type &#39;java.util.Map&#39;: no matching editors or conversion strategy found. 

@Value(&quot;#{${test.feature} ?: { : }}&quot;)

@Value(&quot;#{${test.feature:}}&quot;)

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(&quot;#{${populate.map:{&#39;k1&#39;:&#39;v1&#39;,&#39;k2&#39;:&#39;v2&#39;}}}&quot;)  // result is {&quot;k1&quot;:&quot;v1&quot;,&quot;k2&quot;:&quot;v2&quot;}
private Map&lt;String, String&gt; map3;

or:

@Value(&quot;#{${test.feature:{k1:{nk1:&#39;v1&#39;}}}}&quot;) Map&lt;String,Map&lt;String,String&gt;&gt; map

otherwise:

@Value(&quot;#{${test.feature:{T(java.util.Collections).emptyMap()}}}&quot;) Map&lt;String, String&gt; map3

答案2

得分: 0

  1. 这是如何在.properties文件中定义映射的方式:

    myMap.key1 = value1

    myMap.key2 = value2

    myMap.key3 = value3

  2. 以及如何处理嵌套映射:

    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(&quot;#{${test.feature:{T(java.util.Collections).emptyMap()}}}&quot;)
Map&lt;String, Map&lt;String, String&gt;&gt; ids) {
return ids;

huangapple
  • 本文由 发表于 2023年6月12日 15:21:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定