spring 我想以map的形式读取YAML,我该如何做?

i34xakig  于 2024-01-05  发布在  Spring
关注(0)|答案(1)|浏览(143)

示例yaml看起来像

  1. "mappings":
  2. "name": "foo"
  3. "aliases": "abc"

字符串
尝试使用PropertySourceFactory实现它,但不成功。

  1. import import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.PropertySource;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. @Configuration
  10. //@ConfigurationProperties
  11. @PropertySource(value = "classpath:order_config.yml", factory = YamlPropertySourceFactory.class)
  12. public class ValidatorConfig {
  13. @Value("${yaml.mappings}")
  14. private Map<String, String> mappings = new HashMap<>();
  15. @Value("${yaml.mappings.name}")
  16. private String create;
  17. public String getValidatorBean(String tenant, String requestType) {
  18. System.out.println(mappings);
  19. return "yes";
  20. }
  21. }
  22. import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
  23. import org.springframework.core.env.PropertiesPropertySource;
  24. import org.springframework.core.env.PropertySource;
  25. import org.springframework.core.io.support.EncodedResource;
  26. import org.springframework.core.io.support.PropertySourceFactory;
  27. import java.io.IOException;
  28. import java.util.Properties;
  29. public class YamlPropertySourceFactory implements PropertySourceFactory {
  30. @Override
  31. public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)
  32. throws IOException {
  33. YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
  34. factory.setResources(encodedResource.getResource());
  35. Properties properties = factory.getObject();
  36. return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
  37. }
  38. }


已经尝试过使用一堆方法使用@Value,@ pagationProperties,但不成功我们可以实现它使用YamlMapFactoryBean.还没有能够找到它的工作演示.

suzh9iv8

suzh9iv81#

  1. There are 2 ways to do it.
  2. First setup the configmap.yaml file as shown below--
  3. Key1:
  4. abc: 'abcval'
  5. xyz: 'xyzval'
  6. Key2:
  7. aaa: 'aaaval'
  8. bbb: 'bbbval'
  9. Add to a config class-
  10. @Configuration
  11. public class Configfile{
  12. @Bean
  13. public Map<String, Map<String,String>> configMap() {
  14. Map<String, Map<String,String>> mapVal=new HashMap<String, Map<String,String>>();
  15. ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
  16. InputStream resource = getClass().getResourceAsStream("/configmap.yaml");
  17. mapVal = mapper.mapVal(resource, Map.class);
  18. return mapVal;
  19. }
  20. }
  21. 1) if you know the key
  22. In Service class -
  23. Use the @Value annotation and get in form of map...
  24. many google links have it like this- https://stackoverflow.com/questions/72040438/spring-boot-map-a-map-from-application-yaml-to-a-map-with-value-annotation
  25. 2) if you dont know the key and want to lookup the prop values based on the key runtime
  26. In Service class -
  27. @Resource(name="configMap")
  28. private Map<String,Map<String,String>> configMap;
  29. String keyname = getKeyName()
  30. Map<String,String> keyFieldsmap= configMap.get(keyname);

字符串

展开查看全部

相关问题