英文:
Spring @Value - Is there a way to assign a default empty map?
问题
Situation
我在我的类中使用了@PropertySource和@Value,以便从属性文件中注入值到一个映射中。
用于此操作的代码如下:
@Value("#{${global.fieldsSubset}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
如果找不到所需属性,我希望将映射初始化为空映射。为此,我将代码更改为:
@Value("#{${global.fieldsSubset : {}}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
Problem
默认行为正常工作,当找不到属性时,映射为空。但是当属性存在时,默认值仍然保留,映射始终为空。
以下是整个类的代码:
@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());
}
}
以下是属性值:
global.fieldsSubset = {\
'channel-basic':{'id', 'number', 'name'}, \
'channel':{'id', 'number', 'name', 'description', 'status'}\
}
英文:
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'}\
}
答案1
得分: 4
@Value("#{${global.fieldsSubset:{T(java.util.Collections).emptyMap()}}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
英文:
Try this:
@Value("#{${global.fieldsSubset:{T(java.util.Collections).emptyMap()}}}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
答案2
得分: 1
尝试使用 Elvis 运算符
@Value("#{${global.fieldsSubset} ?: { : }}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
英文:
Try using the Elvis operator
@Value("#{${global.fieldsSubset} ?: { : }}")
private final Map<String, Set<String>> fieldsSubset = new HashMap<>();
答案3
得分: 0
我认为您有2个错误:
- 在字段声明中,
final
是错误的。字段必须是非 final 的,这样 Spring 才能在构建 bean 实例后设置其值。 - JSON 格式错误。
Set
映射到 JSON 列表,因此 JSON 应该是例如'channel':['id', 'number', 'name', 'description', 'status']
,这意味着您应该使用括号而不是大括号来表示映射的值。
英文:
I think you have 2 errors:
- In the declaration of the field, the
final
is wrong. The field must be non-final, so that Spring can set its value after constructing the bean instance - The JSON is wrong. A
Set
is mapped to a JSON list, so the JSON should be i.e.'channel':['id', 'number', 'name', 'description', 'status']
, meaning you should use brackets instead of braces for the values of the map
答案4
得分: 0
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;
更多关于默认值的信息:https://www.baeldung.com/spring-value-defaults
英文:
@Value("#{systemProperties['some.key'] ?: 'my default system property value'}")
private String spelWithDefaultValue;
More info about default values: https://www.baeldung.com/spring-value-defaults
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论