How does one populate a map of values using the @Values
annotation, without defining anything in applicationContext.xml or any other XML file.
I am using spring boot, which doesn't have any XML files, and nor do I want any XML files, so please don't tell me to declare any property reader beans in XML etc.
Also, this is a properties injection question - please don't suggest using a database to store the data - that's not an answer, and not possible for my situation anyway.
Also, I can't use YAML either (due to deployment/operational requirements).
I have tried declaring this injection:
@Value("${myprop}")
Map<Integer, String> map;
And this one
@Value("${myprop.*}")
Map<Integer, String> map;
with these entries application.properties
:
myprop.1=One
myprop.2=Two
myprop.3=Three
and then tried
myprop[1]=One
myprop[2]=Two
myprop[3]=Three
But no good - just explodes with
Could not autowire field: ... Could not resolve placeholder 'myprop'
I have found a work-around with an injected String[]
specified as key1:value1,key2:value2,...
that I then parse in code, but I'd prefer to not do that because a) it's more code, and b) the list is going to be quite long, and all pairs on one line is going to be hard to read and maintain.
Is there a way to automatically build a map from several properties?
I don't care what the property names are, what the field type or the annotation is; I'm just trying to inject one key/value pair per property.