Spring: <map> tag order

I need to embed the map in the bean property and when the map entries are passed, it should return them in the order of placement. In Java, this is similar to LinkedHashMap. But since I cannot find anything in the spring documentation related to tag ordering, I am not sure if I can use it in this scenario.

Can someone please tell me if I can use for this purpose.

Many thanks

+7
source share
2 answers

Default LinkedHashMap - MapFactoryBean used to create Map instances. According to the docs:

A simple factory for shared map instances. Allows you to centrally configure the Map through the "map" element in the XML bean definitions.

and its setTargetMapClass method:

Set the class for the target map. The qualified class name may be completely filled if it is defined in the context of the Spring application.
By default, a linked HashMap is used, preserving the registration order.

See also: LinkedHashMap

therefore there is no need to use <util:map> here.

+8
source

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 a card using ordered keys. You can then use this map using <ref id="mymap" /> , or you can use this construct directly when declaring the value of the Map property.

+8
source

All Articles