【发布时间】:2015-04-28 13:57:28
【问题描述】:
我的属性文件内容如下
config.entry[0]=X-FRAME-OPTIONS,SAMEORIGIN
config.entry[1]=Content-Security-Policy,Frame-Ancestors 'self'
我希望将其加载到一个配置类中,我可以在其中将逗号分隔值加载为集合中的键值对。如何在 Spring 3 中使用 @ConfigurationProperties 来实现这一点?
Entries = config.getEntry() 应该给我一个集合,以便我可以迭代并获取属性文件中定义的名称值对列表
for(Entry<String,String> index : config.getEntry().entryset()) {
index.getKey(); // Should Give - X-FRAME-OPTIONS
index.getValue(); // Should Give - SAMEORIGIN
}
我应该如何定义我的 Config 类,以这种方式使用属性文件中的值自动装配?
下面的实现,抛出 Spring 异常 "Cannot convert value of type [java.lang.String] to required type [java.util.Map]" for property 'entry[0]': no找到匹配的编辑器或转换策略]
@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
}
【问题讨论】:
-
一个键可以有多个值吗?
-
不,每个键只能有 1 个值。我已经用我目前的方法和确切的错误更新了这个问题。
标签: java spring configuration-files