【发布时间】:2015-08-21 21:42:28
【问题描述】:
如何在 Spring 中使用 @Value 注释从属性文件中将值注入 Map?
我的 Spring Java 类是,我尝试使用 $,但收到以下错误消息:
无法自动装配字段:私有 java.util.Map Test.standard;嵌套异常是 java.lang.IllegalArgumentException:无法解析字符串值“${com.test.standard}”中的占位符“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
【问题讨论】:
-
你需要使用spring表达式语言。使用列表(stackoverflow.com/questions/27390363/…)的类似问题。我不确定您是否可以开箱即用地做您想做的事情。这个问题stackoverflow.com/questions/28369458/… 更符合您的观点。使用自定义属性映射器
-
您的地图到底想要什么?看来您还期望某种类型转换为
Pattern?这是什么Pattern类? -
@Erlandsson 这是一个正则表达式模式,我们将在值中定义有效的正则表达式模式字符串
-
@LaurentuiL uin Spring boot,如果映射与类级别描述的前缀匹配,我可以直接注入映射,但是,我的问题是类级别的前缀并且此属性级别不同跨度>
标签: java spring dependency-injection annotations spring-annotations