2

I have an injected class called FooConfig and it is used to get application properties in the following way:

@Value("#{applicationProperties['foo.someProp']}")
private String someProp;

public String getSomeProp() {
    return this.someProp;
}

The actual properties are in a file foo.properties and it is loaded in the injected bean in my context.xml (usual Spring format).

My question is simple: rather than getting singular values, can I specify a properties collection by a prefix (or regex) so that it gets all the properties that match in a Map?

E.g.

//@ValueMap is obviously made up for illustration
@ValueMap("#{applicationProperties['foo.prefix.*']}")
private Map<String, String> somePropMap;

public Map<String, String> getSomePropMap() {
    return this.somePropMap;
}

which would return a map of all properties whose name starts with foo.prefix.

I know that I can just have a method returning the map and in it add properties one by one that are wired the same way as in the first example but I am looking for a bulk shortcut.

amphibient
  • 29,770
  • 54
  • 146
  • 240
  • Is this previous question useful? https://stackoverflow.com/questions/28369458/how-to-fill-hashmap-from-java-property-file-with-spring-value – Assafs Jul 05 '17 at 06:57

0 Answers0