如何在 Spring 中使用 @Value 注释从属性文件中将值注入(inject) Map?
我的 Spring Java 类是,我尝试使用 $,但收到以下错误消息:
Could not autowire field: private java.util.Map Test.standard; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.test.standard' in string value "${com.test.standard}"
@ConfigurationProperty("com.hello.foo")
public class Test {
@Value("${com.test.standard}")
private Map<String,Pattern> standard = new LinkedHashMap<String,Pattern>
private String enabled;
}
我在 .properties 文件中有以下属性
com.test.standard.name1=Pattern1
com.test.standard.name2=Pattern2
com.test.standard.name3=Pattern3
com.hello.foo.enabled=true
最佳答案
您可以像这样使用 @Value
注释从属性文件中将值注入(inject) Map。
属性文件中的属性。
propertyname={key1:'value1',key2:'value2',....}
在您的代码中。
@Value("#{${propertyname}}") private Map<String,String> propertyname;
请注意标签作为注释的一部分。
关于java - 如何使用@Value Spring Annotation 注入(inject) Map?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30691949/