I have a properties file like this:
my.properties file:
app.One.id=1
app.One.val=60
app.Two.id=5
app.Two.val=75
And I read these values into a map property in my bean in Spring config file like this:
spring-config.xml:
<bean id="myBean" class="myClass" scope="singleton">
<property name="myMap">
<map>
<entry key="${app.One.id}" value="${app.One.val}"/>
<entry key="${app.Two.id}" value="${app.Two.val}"/>
</map>
</property>
</bean>
This way if I add a new id/val to the properties file, I must add a row in config xml in order to have the new id/val in myMap.
My question is, is there a way to specify the key-val pairs in spring config file so that the number of key-vals defined in xml can figure out the items in the properties file and create a map. Basically I want to use this xml file in different environments where we use different number of key-value items in properties file. I just don't want to change the xml file in each environment to read in all these values.
Let me know if you need any other details. Any thoughts/comments is appreciated. Thanks!
It's a classic environment problem.
There are two ways to do this:
I did not find an ideal way to solve this issue to have dynamic map property read into spring configuration info. Using DB was also not an option for us. This is what I found to be the best alternative for this:
Use a standard format for map key/value pair in the properties file eg: key1 | val1, key2 | val2, key3 | val3, ..., keyn | valn
Read this to a String property or List property in configuration bean like here: Use String to List
Let injected java class split the items into map in setter.
I'm going to mark this as answer. If anyone else has better ways to do this, comment it out and I will change it to answer.
This is done with Spring EL and custom handling.
It was just interesting for me to try it. It works :)
application.xml
Utils.java
Hello.java
values.properties
Maybe I did not fully understand the issue here...
What about a simplified approach?
Application Context
Java Bean