Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring : order of <map> tag

Tags:

java

spring

map

I need to Inject a Map to a bean property and when map entries are traversed it should return them in insertion order. In Java, this is similar to the LinkedHashMap. But as I can't find anything in spring documentation related to the ordering of tag, I'm not sure whether I can use to use it in this scenario.

Can someone please let me know the whether I can use for this purpose.

Many thanks

like image 884
rKasun Avatar asked Apr 11 '12 10:04

rKasun


1 Answers

Use this construct:

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

...
<util:map id="mymap" map-class="java.util.LinkedHashMap">
    <entry key="a" value="b" />
    <entry key="c" value="d" />
</util:map>

...
</beans>

to declare the map with ordered keys. You can then use this map using <ref id="mymap" /> or you can use this construct directly while declaring the value of Map property.

like image 158
Grzegorz Grzybek Avatar answered Oct 04 '22 15:10

Grzegorz Grzybek