Reading a dynamic property map into Spring managed bean

后端 未结 4 2112
悲哀的现实
悲哀的现实 2021-02-05 23:53

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

4条回答
  •  长发绾君心
    2021-02-06 00:40

    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.

提交回复
热议问题