user3233964 user3233964 - 2 months ago 22
Java Question

Default return value for Spring map

How do I set a default value for a util:map in the spring framework?

example:
If I pass "thisThing" I would like it to go to value-ref "call_to_default".

<util:map id= "someID" map-class="org.apache.commons.collections4.map">
<constructor-arg value= "call_to_default"/>
<entry key="someStuff" value-ref="call_to_someStuff">


I am not looking to pass it "default" though, I would like to pass it anything except "someStuff" and have it call default call. The code equivalent would be using getOrDefault method. I would like to do this in spring though.

Is this possible? How do I accomplish this?

Answer

You can't do this in Spring, I'm afraid.

That said, in Java 8, Maps have a getOrDefault method which does what you're describing. So if you specify a default value as a different property of your bean (the bean that uses the map, for clarity), you can integrate it with this method.

Comments