【问题标题】:How to load Name Value property using @ConfigurationProperties in Spring如何在 Spring 中使用 @ConfigurationProperties 加载名称值属性
【发布时间】: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


【解决方案1】:

您可以尝试使用 @PostConstruct 注释,该注释将在构造 bean 之后调用。您可以在此处加载配置文件并根据需要更新地图。

例如:

@Component
public class Config {
    private Map<String, String> entry = new LinkedHashMap<String, String>();

    @PostConstruct
    private void init() throws IOException {
        try (InputStream input = ClassUtils.getDefaultClassLoader().getResourceAsStream("config.properties")) {
            for (String line : IOUtils.readLines(input)) {
                // change the logic as per your need
                String[] keyValue = line.split("=")[1].split(",");
                entry.put(keyValue[0], keyValue[1]);
            }
        }
    }
}

【讨论】:

  • @ConfigurationProperties 不允许直接加载这种名称-值对值的方法吗? ..这将是最终选择
  • 我不这么认为,因为这是您自己的自定义文件,不符合全球要求。
  • 这种方法有问题吗?你能告诉我为什么它是最终选择吗?
  • Spring 将整个文本作为 1 个字符串,并以 0、1 作为键创建该字符串的 Map。相反,如何强制 spring 将文本的第一部分用作 Key 并将文本的第二部分用作 value .. 而不是将索引用作 Key ?
  • 为什么不能使用简单的属性文件和键值对?让它变得简单。
【解决方案2】:

您可以分两部分执行此操作,首先是简单的@ConfigurationProperties

@ConfigurationProperties(prefix = "config")
@Component
public class SampleConfigProperties {

    private List<String> entry;

    public List<String> getEntry() {
        return entry;
    }

    public void setEntry(List<String> entry) {
        this.entry = entry;
    }
}

这应该会将您的属性干净地加载到您的 SampleConfigPropertiesentry 字段中。你接下来要做的不是 Spring 原生的 - 你希望逗号分隔列表中的第一个字段成为映射中的键,这是一个自定义逻辑,你可以使用 @PostConstruct 逻辑这样处理这个逻辑,看看我如何仍在使用 Spring 的 conversionService 将逗号分隔的 String 转换为 List&lt;String&gt;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ConfigurationProperties(prefix = "config")
@Component
public class SampleConfigProperties {

    @Autowired
    private ConversionService conversionService;
    private List<String> entry;

    private Map<String, String> entriesMap;


    public List<String> getEntry() {
        return entry;
    }

    public void setEntry(List<String> entry) {
        this.entry = entry;
    }

    public Map<String, String> getEntriesMap() {
        return entriesMap;
    }

    public void setEntriesMap(Map<String, String> entriesMap) {
        this.entriesMap = entriesMap;
    }

    @PostConstruct
    public void postConstruct() {
        Map<String, String> entriesMap = new HashMap<>();
        for (String anEntry: entry) {
            List<String> l = conversionService.convert(anEntry, List.class);
            entriesMap.put(l.get(0), l.get(1));
        }
        this.entriesMap = entriesMap;
    }

}

【讨论】:

  • 需要额外的帮助。当字符串用逗号 (,) 分隔时,>> List l
猜你喜欢
  • 2019-08-09
  • 1970-01-01
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 1970-01-01
相关资源
最近更新 更多