brownac
brownac

Reputation: 71

Spring Boot - Apache Commons Configuration - How to get properties with the same suffix

I have an application.properties file which contains some properties like so:

foo.name=Some name
foo.link=https://www.example.com
foo.traits=a trait,another one,and another

...

bar.name=Another name
bar.link=https://www.anotherexample.com
bar.traits=some more traits,and some more,this is getting old

How can I get properties which share a similar suffix? For example, I want to stuff foo.name and bar.name into a list/array; similarly with the other properties. I've seen this done with properties that share a prefix, but never a suffix. Is regex the best approach? Or does this library have some function I haven't been able to find?

A note: this needs to be done dynamically; so, if someone were to add another property, say blah.name, it would be stuffed into the already existing array which already contains foo and bar.name.

Thanks in advance.

Upvotes: 1

Views: 1798

Answers (1)

geneSummons
geneSummons

Reputation: 925

I don't think you can group property key/value pairs by the key's "suffix" out of the box. I think you need to implement a customization of a PropertyResolver or similar. Where you would scan all the available properties, pick the ones with the suffix of interest, load the interesting key/value pairs into a Map, and then inject a new dynamic custom Map property back into the Environment

Yes, a regex would be the route I'd take to identify candidate properties. Something like ^(.*\.)(.*)=(.*)$ where group 1 is the property key prefix, group 2 is the suffix of interest, and group 3 is the property value.

Upvotes: 1

Related Questions