How to define a map property in a separate XML file (in Spring)?

Belun picture Belun · Dec 15, 2010 · Viewed 17.1k times · Source

Suppose a project uses Spring and defines it's beans within XMLs ? And it has some bean that accepts a Map in constructor.

Usually, this map is defined as a property under the bean, and has, under it, entries.

But what if the entry list is huge ? It will bloat the XML big time...

Can it (the map) somehow be defined in it's on XML file and then refferenced by the bean that needs it ? How ?

Answer

skaffman picture skaffman · Dec 15, 2010

Yes, using the <util:map> syntax (see docs), e.g.

beans1.xml

<util:map id="myMap">
    <entry .../>
    <entry .../>
    <entry .../>
    <entry .../>
</util:map>

beans2.xml

<import resource="beans1.xml"/>

<bean id="..." class="...">
   <constructor-arg ref="myMap"/>
</bean>