读取yml文件里的list配置

读取yml文件里的list配置

YAML 支持以下几种数据类型:

对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
纯量(scalars):单个的、不可再分的值

这里只介绍list类型的读取
yml里的list配置(以 - 开头的行表示构成一个数组:):

weixin:
  configs:
  	- schId: 111
      appId: 11111
      appSecret: 11111
      templateId: 111111
    - schId: 2222
      appId: 222222
      appSecret: 2222222
      templateId: 2222222

导入jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

写一个微信配置的实体类,将配置文件中配置的每一个属性的值,映射到这个实体类中

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;

@Component
@Data
@ConfigurationProperties(prefix = "weixin")
public class WxConfig {
    private List<Config> configs;
    @Data
    public static class Config {
        private Integer schId;
        private String appId;
        private String appSecret;
        private String templateId;
    }
}

拿到配置文件里的内容

1.注入该实体类

 @Autowired
    private WxConfig wxConfig;

2.解析,我这里是将其转为学校id为key的map,方便我后面使用,按照自己想要的读取就好了JSON.toJSONString(wxConfig)

    public  Map<Integer,Map> getWeiXinConfig(){
        JSONObject o = JSON.parseObject(JSON.toJSONString(wxConfig));
        JSONArray jsonArray = o.getJSONArray("configs");
        Map<Integer,Map> map = new HashMap<>();
        if (jsonArray.size() != 0) {
            for (int j = 0; j < jsonArray.size(); j++) {
                Map map2 = new HashMap();
                JSONObject o1 = jsonArray.getJSONObject(j);
                String appId = o1.getString("appId");
                String appSecret = o1.getString("appSecret");
                Integer schId = o1.getIntValue("schId");
                String templateId = o1.getString("templateId");
                map2.put("appId", appId);
                map2.put("appSecret", appSecret);
                map2.put("schId", schId);
                map2.put("templateId", templateId);
                map.put(schId,map2);
            }
        }
        return map;
    }

调用方法getWeiXinConfig(),输出map的输出结果:
在这里插入图片描述

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在application.yml配置list时,需要使用"-"来组成一个列表集合。同时,属性名称在yml文件中支持连字符"-",比如"four-span",而在Java类中配置属性时需要转为驼峰式,如"fourSpan"。此外,还需要在Java类中配置set和get方法。可以使用@ConfigurationProperties注解来指定配置类的前缀,如@ConfigurationProperties(prefix = "demo.code")。另外,在配置类中需要使用@Configuration注解标记该类为配置类,并使用@Getter和@Setter注解为属性生成get和set方法。在配置类中,可以定义一个List属性来接收yml文件中的列表值。例如,可以创建一个配置类Config,其中包含一个paramInfo属性用来接收列表值,代码如下: ```java @Configuration @ConfigurationProperties(prefix = "demo.code") @Getter @Setter public class Config { private List<ParamInfo> paramInfo; } ``` 这样,通过读取application.yml文件中的配置,并将列表值注入到Config类的paramInfo属性中,以便在应用程序中使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [springboot读取yml文件中的list列表、数组、map集合和对象](https://blog.csdn.net/weixin_42274846/article/details/128155678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [springboot的application.yml如何配置List、Map形式的配置](https://blog.csdn.net/Hope_lee/article/details/105660434)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值