I have a class like this:
class MyClass {
Map<String, String[]> arrays;
public void setArrays(Map<String, String[]> arrays)
{
this.arrays = arrays;
}
public String[] getArray(String key)
{
return arrays.get(key);
}
}
The values are provided from a properties file like this:
# my.properties
arrays.arrayOne=a,b,c
arrays.arrayTwo=d,e,f
Using spring I can wire the property this way:
<property name="arrays">
<map>
<entry key="arrayOne" value="${arrays.arrayOne}"/>
<entry key="arrayTwo" value="${arrays.arrayTwo}"/>
</map>
</property>
Now, this works but I have to manually edit the wiring every time I add a new entry into the properties file. Is there a better way to do this?
I solved my problem using PropertyOverrideConfigurer:
(I could also init the map directly in my class to make the code more concise)
That’s all it takes, and spring populates the map correctly, additions to the properties file being updated without further config. Calling getArray(“arrayOne”) on my bean returns an array of strings {“a”, “b”, “c”} as intended.