user3233964 user3233964 - 11 months ago 99
Java Question

Default return value for Spring map

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

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

<util:map id= "someID" map-class="">
<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 Source

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.