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 ?
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With