Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 472
Belun Avatar asked Dec 15 '10 14:12

Belun


2 Answers

skaffman's answer worked for me. However, to setup the XML namespaces, beans1.xml should look like:

<?xml version="1.0" encoding="UTF-8"?>
<util:map id="myMap" 
    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-3.1.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util.xsd">

    <entry key="myKey" value="myValue" />

</util:map>
like image 55
Arlo Avatar answered Oct 20 '22 08:10

Arlo


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>
like image 36
skaffman Avatar answered Oct 20 '22 08:10

skaffman