在spring boot web项目中将yaml文件加载到Map(不是环境配置文件)的最佳方法是什么? [英] What's the best way to load a yaml file to Map(not an environment configuration file) in spring boot web project?

查看:55
本文介绍了在spring boot web项目中将yaml文件加载到Map(不是环境配置文件)的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据框架层,我想从 src/main/resources 读取一个 yaml.文件名为 mapconfigure.yaml.它与业务数据相关联,而不仅仅是环境配置数据.

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.

它的内容是这样的:

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

我想将此信息存储到 HashMap 中.

I want to store this information into a HashMap.

是否意味着使用一些像@ConfigurationProperties这样的spring注解?如何详细实现这一点?

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

另外,我无法更改文件名.这意味着我必须使用 mapconfigure.yaml 作为文件名,而不是 application.ymlapplication.properties.

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.

我的HashMap的结构如下:

The structure of my HashMap is as follows:

HashMap<String, Setting>

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

预期的 HashMap 如下:

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

我不确定是否可以使用 YamlMapFactoryBean 类来做到这一点.

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

YamlMapFactoryBean类中getObject方法的返回类型是Map,不是泛型类型,如Map.

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 刚刚说的

Spring Framework 提供了两个方便的类,可用于加载 YAML 文档.YamlPropertiesFactoryBean 将 YAML 作为属性加载,YamlMapFactoryBean 将 YAML 作为 Map 加载.

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.

但是没有详细的例子.

更新:

在 github 中,我创建了一个示例.就在这里.在此示例中,我想将 myconfig.yaml 加载到 SamplePropertyLoadingTest 类中的 theMapProperties 对象.Spring boot 版本是 1.5.1,所以我不能使用 @ConfigurationPropertieslocation 属性.如何做到这一点?

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?

推荐答案

你确实可以通过@ConfigurationProperties 实现这一点.

You can indeed achieve this with @ConfigurationProperties.

从 Spring Boot 1.5.x 开始(缺少 @ConfigurationProperies 位置属性):

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
}

在 Spring Boot 1.3.x 中:

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
}

以上示例的 Person 类如下所示:

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
}

我已经使用以下文件测试了代码:your-filename.yml定义在src/main/resources中,内容:

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.

这篇关于在spring boot web项目中将yaml文件加载到Map(不是环境配置文件)的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆