Situation
I am using @PropertySource in my class along with @Value in order to inject a value to a map from a properties file. The code for that is as follows:
@Value("#{${global.fieldsSubset}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
In case the required property isn't found I'd like to initialize the map as an empty map. For that I changed the code to:
@Value("#{${global.fieldsSubset : {:}}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
Problem
The default behavior works fine and the map is empty when the property isn't found, but when the property is present the default value still remains and the map is always empty.
Here is the whole class:
@Configuration
@PropertySource(value = "classpath:global.properties", ignoreResourceNotFound = true)
@RefreshScope
public class FieldsSubsetsProperties {
@Value("#{${global.fieldsSubset:{:}}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
public Map<String, Set<String>> getFieldsSubset() {
return fieldsSubset;
}
public Set<String> getSubsetOfFieldsNames(String subsetName) {
return fieldsSubset.getOrDefault(subsetName,
Collections.emptySet());
}
}
Here is the property value:
global.fieldsSubset = {\
'channel-basic':{'id', 'number', 'name'}, \
'channel':{'id', 'number', 'name', 'description', 'status'}\
}