How to load Value Name property using @ConfigurationProperties in Spring
My properties file has content
config.entry [0] = X-FRAME-OPTIONS, SAMEORIGIN
config.entry [1] = Content-Security-Policy, self-self-ancestors
I want this to be loaded into a config class where I can have comma separated values loaded as Key, Value pairs in the collection. How can this be achieved with @ConfigurationProperties in Spring 3?
Entries = config.getEntry () should give me a collection so that I can iterate and get a list of name value pairs defined in properties file
for(Entry<String,String> index : config.getEntry().entryset()) {
index.getKey(); // Should Give - X-FRAME-OPTIONS
index.getValue(); // Should Give - SAMEORIGIN
}
How should I define my Config class to be automatically associated with the values from the properties file?
following implementation, throws Spring exception "Cannot convert value of type [java.lang.String] to required type [java.util.Map]" for property 'entry [0]': no match editors or conversion strategy]
@Component
@ConfigurationProperties("config")
public class MyConfiguration {
private Map<String,Map<String,String>> entry = new LinkedHashMap<String,Map<String,String>>();
//getter for entry
//setter for entry
}
source to share
You can do it in two parts, first just @ConfigurationProperties
like this:
@ConfigurationProperties(prefix = "config")
@Component
public class SampleConfigProperties {
private List<String> entry;
public List<String> getEntry() {
return entry;
}
public void setEntry(List<String> entry) {
this.entry = entry;
}
}
This should cleanly load your properties into entry
your field SampleConfigProperties
initially. What you want to do next is not native to Spring - you want the first field in the comma delimited list to be the key in the map, this is custom logic, you can handle this logic using @PostConstruct
this logic By the way, how am I still using Spring conversionService
to convert semicolon separator String
to List<String>
:
import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ConfigurationProperties(prefix = "config")
@Component
public class SampleConfigProperties {
@Autowired
private ConversionService conversionService;
private List<String> entry;
private Map<String, String> entriesMap;
public List<String> getEntry() {
return entry;
}
public void setEntry(List<String> entry) {
this.entry = entry;
}
public Map<String, String> getEntriesMap() {
return entriesMap;
}
public void setEntriesMap(Map<String, String> entriesMap) {
this.entriesMap = entriesMap;
}
@PostConstruct
public void postConstruct() {
Map<String, String> entriesMap = new HashMap<>();
for (String anEntry: entry) {
List<String> l = conversionService.convert(anEntry, List.class);
entriesMap.put(l.get(0), l.get(1));
}
this.entriesMap = entriesMap;
}
}
source to share
You can try with annotation @PostConstruct
that will be called after the bean in the constructed one. Here you can download the config file and update the map according to your needs.
For example:
@Component
public class Config {
private Map<String, String> entry = new LinkedHashMap<String, String>();
@PostConstruct
private void init() throws IOException {
try (InputStream input = ClassUtils.getDefaultClassLoader().getResourceAsStream("config.properties")) {
for (String line : IOUtils.readLines(input)) {
// change the logic as per your need
String[] keyValue = line.split("=")[1].split(",");
entry.put(keyValue[0], keyValue[1]);
}
}
}
}
source to share