關(guān)于springBoot yml文件的list讀取問題總結(jié)(親測(cè))
springBoot yml文件的list讀取問題
折騰了很久,記錄下。
配置如下
# 自定義數(shù)據(jù)上報(bào)信息
xx:
# 機(jī)組信息
machine1s:
- name: XXXX
frequency: null
frequency-unit: null
pressure: 0.01
pressure-unit: Pa
flow: 0
flow-unit: NM3/H
state: 停機(jī)
runtime: 17.5
runtime-unit: 天
- name: XXXX
frequency: 42.4
frequency-unit: HZ
pressure: 0.39
pressure-unit: Pa
flow: 4730
flow-unit: NM3/H
state: 運(yùn)行
runtime: 12.5
runtime-unit: 天
- name: XXXX
frequency: 46.4
frequency-unit: HZ
pressure: 0.00
pressure-unit: Pa
flow: 0
flow-unit: NM3/H
state: 停機(jī)
runtime: 8.2
runtime-unit: 天
- name: XXXX
frequency: 41.4
frequency-unit: HZ
pressure: 0.39
pressure-unit: Pa
flow: 9532
flow-unit: NM3/H
state: 運(yùn)行
runtime: 3.2
runtime-unit: 天
- name: XXXX
frequency: null
frequency-unit: null
pressure: 0.38
pressure-unit: Pa
flow: 4800
flow-unit: NM3/H
state: 停機(jī)
runtime: 20.4
runtime-unit: 天
- name: XXXX
frequency: null
frequency-unit: null
pressure: 0.01
pressure-unit: Pa
flow: 0
flow-unit: NM3/H
state: 停機(jī)
runtime: 7.5
runtime-unit: 天
1.定義配置類
@ConfigurationProperties(prefix = "xx")
public class TXMachinesProperties {
private List<Map<String, String>> machine1s;
public List<Map<String, String>> getMachine1s() {
return machine1s;
}
public void setMachine1s(List<Map<String, String>> machine1s) {
this.machine1s = machine1s;
}
}
注意:
a.這里prefix寫到接收對(duì)象的前一級(jí)即可;
b.這里的變量名必須要與配置的名稱一致,才可自動(dòng)接收。
2.定義啟動(dòng)的配置類
這里其實(shí)可與上面的配置類寫在一起,但是一個(gè)類就是做一件事情,就做了隔離。
@Configuration
@EnableConfigurationProperties({TXMachinesProperties.class})
public class TXMachinesConfig {
}
3.使用方式
采用下面的方式即可。這里注意,由于使用這個(gè)yml的注解是屬于SpringBoot的框架內(nèi)進(jìn)行的,因此這個(gè)屬性注入只能在標(biāo)有Spring的注解的類的范圍內(nèi)使用,不能在普通類使用。
@Autowired
private TXWorkShopAlarmProperties txWorkShopAlarmProperties;
可以解決了~
讀取yml文件里的list配置
YAML 支持以下幾種數(shù)據(jù)類型
- 對(duì)象:鍵值對(duì)的集合,又稱為映射(mapping)/ 哈希(hashes) / 字典(dictionary)
- 數(shù)組:一組按次序排列的值,又稱為序列(sequence) / 列表(list)
- 純量(scalars):單個(gè)的、不可再分的值
這里只介紹list類型的讀取
yml里的list配置(以 - 開頭的行表示構(gòu)成一個(gè)數(shù)組:):
weixin:
configs:
- schId: 111
appId: 11111
appSecret: 11111
templateId: 111111
- schId: 2222
appId: 222222
appSecret: 2222222
templateId: 2222222
導(dǎo)入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
寫一個(gè)微信配置的實(shí)體類,將配置文件中配置的每一個(gè)屬性的值,映射到這個(gè)實(shí)體類中
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;
}
}
拿到配置文件里的內(nèi)容
1.注入該實(shí)體類
@Autowired
private WxConfig wxConfig;
2.解析,我這里是將其轉(zhuǎn)為學(xué)校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;
}
調(diào)用方法getWeiXinConfig(),輸出map的輸出結(jié)果:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mys
這篇文章主要介紹了如何在 Linux 上搭建 java 部署環(huán)境(安裝jdk/tomcat/mysql) + 將程序部署到云服務(wù)器上的操作),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01
Java實(shí)現(xiàn)的兩個(gè)線程同時(shí)運(yùn)行案例
這篇文章主要介紹了Java實(shí)現(xiàn)的兩個(gè)線程同時(shí)運(yùn)行,涉及java多線程相關(guān)操作與使用技巧,需要的朋友可以參考下2019-07-07
spring中ApplicationListener的使用小結(jié)
ApplicationListener是spring提供的一個(gè)監(jiān)聽器,本文主要介紹了spring中ApplicationListener的使用小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Spring Boot整合logback一個(gè)簡單的日志集成架構(gòu)
今天小編就為大家分享一篇關(guān)于Spring Boot整合logback一個(gè)簡單的日志集成架構(gòu),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
Java使用Apache POI庫讀取Excel表格文檔的示例
POI庫是Apache提供的用于在Windows下讀寫各類微軟Office文檔的Java庫,這里我們就來看一下Java使用Apache POI庫讀取Excel表格文檔的示例:2016-06-06

