0

The following is legal Spring config:

<util:map id="beanMap">
    <entry key="key1" value-ref="bean1"/>
    <entry key="key2" value-ref="bean2"/>
    <entry key="key3" value-ref="bean3"/>
</util:map>

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

I would like to externalise this map to a .properties file (or any other kind of file) for deployment config / environment reasons, for example:

<util:properties id="beanProperties" location="file:/bean-value.properties"/>

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

This doesn't work out of the box, as a Properties object is effectively a Map<String, String>, so this needs to be converted somehow, or use a way to tell Spring, that the values are in fact "value-ref" bean names.

Is there an easy / recommended way to achieve this?

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • Take a look at my reply for [this SO post](http://stackoverflow.com/questions/25236748/injecting-a-map-of-properties-via-value-alone/25304973#25304973). It's an xml-less approach for, I believe, something very similar. – Andrei Stefan Aug 22 '14 at 17:04
  • An xml isn't externalised enough...? You can move the util:map declaration to another file anywhere on the classpath and add it to the config locations in your web.xml – Affe Aug 22 '14 at 17:06
  • 1
    Haven't read the question carefully. Sorry about that. Your map has "value-ref"s to other beans. – Andrei Stefan Aug 22 '14 at 17:06
  • @Affe - There's an established deployment structure in place already. There's a cost to messing with that, just as there's a cost to writing code. Currently the context XML files remain in the .jars at deployment time. Possibly there could be a property which gives the location of the XML for the `util:map` config. Oh, and by the way, it's not a webapp, so there is no web.xml. – Stewart Aug 22 '14 at 17:39
  • Ok, but where were you going to put the properties file within the established deployment structure? You can just put XML there instead and add it to the application context. I just meant to point out you have two problems in your question. How to use properties for that purpose, and how to externalise a file. How to use properties is only a problem if you want it to be, that piece of spring xml could just be externalised itself. – Affe Aug 22 '14 at 21:08

0 Answers0