如何在 Spring 中使用 @ConfigurationProperties 加载名称值 属性

How to load Name Value property using @ConfigurationProperties in Spring

我的属性文件内容如下

config.entry[0]=X-FRAME-OPTIONS,SAMEORIGIN

config.entry[1]=Content-Security-Policy,Frame-Ancestors 'self'

我希望将其加载到配置 class 中,我可以在其中将逗号分隔值作为键、值对加载到集合中。如何在 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
}

我应该如何定义我的配置 class,它将以这种方式与属性文件中的值自动连接?

以下实现,抛出 Spring 异常 "Cannot convert value of type [java.lang.String] to required type [java.util.Map]" 属性 'entry[0]':找不到匹配的编辑器或转换策略]

@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
}

您可以尝试使用 @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 这种方式:

@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 原生的 - 您希望逗号分隔列表中的第一个字段成为映射中的键,这是自定义逻辑,您可以使用 [=15 处理此逻辑=] 这样的逻辑,看看我如何仍然使用 Spring 的 conversionService 将逗号分隔的 String 转换为 List<String>:

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;
    }

}