What's the best way to load a yaml file to Map(not an environment configuration file) in spring boot web project?

笑着哭i 提交于 2021-02-18 22:29:45

问题


In my data framework layer, I'd like to read an yaml from src/main/resources. The file name is mapconfigure.yaml. It's associated with the business data, not just environment configuration data.

Its content's like:

person1: 
  name: aaa
  addresses: 
    na: jiang
    sb: su
person2: 
  name: bbb
  addresses: 
    to: jiang
    bit: su

I want to store this information into a HashMap.

Does it mean to use some spring annotation like @ConfigurationProperties? How to achieve this in details?

In addition, I can't change the file name. It means I have to use mapconfigure.yaml as the file name, not application.yml or application.properties.

The structure of my HashMap is as follows:

HashMap<String, Setting>

@Data
public class Setting{
  private String name;
  private HashMap<String, String> addresses
}

My expected HashMap's as follows:

{person1={name=aaa, addresses={na=jiang, sb=su}}, person2={name=bbb, addresses={to=jiang, bit=su}}}

I'm not sure if I can use YamlMapFactoryBean class to do this.

The return type of the getObject method in YamlMapFactoryBean class is Map<String, Object>, not a generic type, like Map<String, T>.

Spring boot doc just said

Spring Framework provides two convenient classes that can be used to load YAML documents. The YamlPropertiesFactoryBean will load YAML as Properties and the YamlMapFactoryBean will load YAML as a Map.

But there isn't a detailed example.

UPDATE:

In github, I created a sample. It's Here. In this sample, I want to load myconfig.yaml to theMapProperties object in SamplePropertyLoadingTest class. Spring boot version is 1.5.1, so I can't use location attribute of @ConfigurationProperties. How to do this?


回答1:


You can indeed achieve this with @ConfigurationProperties.

From Spring Boot 1.5.x onwards (lack of @ConfigurationProperies locations attr.):

new SpringApplicationBuilder(Application.class)
    .properties("spring.config.name=application,your-filename")
    .run(args);

@Component
@ConfigurationProperties
public class TheProperties {
    private Map<String, Person> people;
    // getters and setters are omitted for brevity
}

In Spring Boot 1.3.x:

@Component
@ConfigurationProperties(locations = "classpath:your-filename.yml")
public class TheProperties {
    private Map<String, Person> people;
    // getters and setters are omitted for brevity
}

The Person class for above examples looks like this:

public class Person {
    private String name;
    private Map<String, String> addresses;
    // getters and setters are omitted for brevity
}

I have tested the code with the following file: your-filename.yml defined in src/main/resources, the contents:

people:
  person1:
    name: "aaa"
    addresses:
      na: "jiang"
      sb: "su"
  person2:
    name: "bbb"
    addresses:
      to: "jiang"
      bit: "su"

Please let me know if you need any further assistance.




回答2:


try this

 YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
        try {
            PropertySource<?> applicationYamlPropertySource = loader.load(
                "properties", new ClassPathResource("application.yml"), null);// null indicated common properties for all profiles.
            Map source = ((MapPropertySource) applicationYamlPropertySource).getSource();
            Properties properties = new Properties();
            properties.putAll(source);
            return properties;
        } catch (IOException e) {
            LOG.error("application.yml file cannot be found.");
        }


来源:https://stackoverflow.com/questions/43092808/whats-the-best-way-to-load-a-yaml-file-to-mapnot-an-environment-configuration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!