1

I wish to create a map within my spring.xml. The closest I can get is a list of lists

<bean id="transformerHelper" class="com.common.TransformerHelper">
    <property name="srcSystemIDList" value="{T(java.util.Arrays).asList({'11','50'},{'41','50'}) }" />
</bean>

However I really need these values in a map where I can return 50 when searching the key 11 or 41. I need this metadata defined in Spring as it will be stored away from the application.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
Will
  • 8,246
  • 16
  • 60
  • 92

3 Answers3

2

See section A.2.2.5 of this doc, and note this example:

<util:map id="emails">
    <entry key="pechorin" value="pechorin@hero.org"/>
    <entry key="raskolnikov" value="raskolnikov@slums.org"/>
    <entry key="stavrogin" value="stavrogin@gov.org"/>
    <entry key="porfiry" value="porfiry@gov.org"/>
</util:map>
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Possible to do it in a one liner otherwise it will mean meta data variable hell – Will Oct 02 '12 at 08:49
  • If you really want to do it in a one-liner without concatenating the XML, you'd likely have to come up with some map-serialisation formatting yourself, and write a class to parse that. I think trying to squeeze such stuff into one-line is going to obfuscate the configuration info – Brian Agnew Oct 02 '12 at 08:50
0

Refer the link: converting-static-2d-string-array-to-hashmap and use init method or implement InitializingBean in your bean class.

Community
  • 1
  • 1
Maheshkumar
  • 724
  • 1
  • 10
  • 19
0

There is a way, I recommend to add another property to your class, a String that will hold all the map values (keys and values), something like you already have:

{'11','50'},{'41','50'},{'12','34'}

Then, instead of injecting the map, inject the string and in it's setter method (if you are using setter injection) call a small method (created by you) that will parse that string and populate the map, use StringTokenizer or what ever you like.

Adolfo
  • 798
  • 1
  • 7
  • 15
  • Ended up doing something similar to this. As the elements were all numbers I stored them in an array and wrote a method similar to StringTokenizer, to transfer the values into a keyed map. – Will Oct 08 '12 at 08:36