如何在spring中使用@configurationproperties加载name-value属性

zaqlnxep  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(446)

我的属性文件的内容如下
config.entry[0]=x-frame-options,sameorigin
config.entry[1]=内容安全策略,框架“self”
我想把它加载到一个config类中,在那里我可以把逗号分隔的值作为键、值对加载到一个集合中。如何在spring3中使用@configurationproperties实现这一点?
entries=config.getentry()应该为我提供一个集合,以便我可以迭代并获取属性文件中定义的名称-值对列表

  1. for(Entry<String,String> index : config.getEntry().entryset()) {
  2. index.getKey(); // Should Give - X-FRAME-OPTIONS
  3. index.getValue(); // Should Give - SAMEORIGIN
  4. }

我应该如何定义将以这种方式与属性文件中的值自动关联的配置类?
对于属性“entry[0]”,以下实现引发了spring异常“cannot convert value of type[java.lang.string]to required type[java.util.map]”:找不到匹配的编辑器或转换策略]

  1. @Component
  2. @ConfigurationProperties("config")
  3. public class MyConfiguration {
  4. private Map<String,Map<String,String>> entry = new LinkedHashMap<String,Map<String,String>>();
  5. //getter for entry
  6. //setter for entry
  7. }
jvlzgdj9

jvlzgdj91#

你可以分两部分来做,第一部分是简单的 @ConfigurationProperties 这种方式:

  1. @ConfigurationProperties(prefix = "config")
  2. @Component
  3. public class SampleConfigProperties {
  4. private List<String> entry;
  5. public List<String> getEntry() {
  6. return entry;
  7. }
  8. public void setEntry(List<String> entry) {
  9. this.entry = entry;
  10. }
  11. }

这将干净地将您的属性加载到 entry 你的领域 SampleConfigProperties 天生的。接下来要做的不是spring的本机操作—您希望逗号分隔列表中的第一个字段是Map中的键,这是一个自定义逻辑,您可以使用 @PostConstruct 这样的逻辑,看看我怎么还在用spring的 conversionService 转换逗号分隔的 StringList<String> :

  1. import javax.annotation.PostConstruct;
  2. import java.util.Collection;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. @ConfigurationProperties(prefix = "config")
  7. @Component
  8. public class SampleConfigProperties {
  9. @Autowired
  10. private ConversionService conversionService;
  11. private List<String> entry;
  12. private Map<String, String> entriesMap;
  13. public List<String> getEntry() {
  14. return entry;
  15. }
  16. public void setEntry(List<String> entry) {
  17. this.entry = entry;
  18. }
  19. public Map<String, String> getEntriesMap() {
  20. return entriesMap;
  21. }
  22. public void setEntriesMap(Map<String, String> entriesMap) {
  23. this.entriesMap = entriesMap;
  24. }
  25. @PostConstruct
  26. public void postConstruct() {
  27. Map<String, String> entriesMap = new HashMap<>();
  28. for (String anEntry: entry) {
  29. List<String> l = conversionService.convert(anEntry, List.class);
  30. entriesMap.put(l.get(0), l.get(1));
  31. }
  32. this.entriesMap = entriesMap;
  33. }
  34. }
展开查看全部
bejyjqdl

bejyjqdl2#

你可以试试 @PostConstruct 将在构造的bean之后调用的注解。您可以在这里加载配置文件并根据需要更新Map。
例如:

  1. @Component
  2. public class Config {
  3. private Map<String, String> entry = new LinkedHashMap<String, String>();
  4. @PostConstruct
  5. private void init() throws IOException {
  6. try (InputStream input = ClassUtils.getDefaultClassLoader().getResourceAsStream("config.properties")) {
  7. for (String line : IOUtils.readLines(input)) {
  8. // change the logic as per your need
  9. String[] keyValue = line.split("=")[1].split(",");
  10. entry.put(keyValue[0], keyValue[1]);
  11. }
  12. }
  13. }
  14. }
展开查看全部

相关问题