Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to summarize the list Reading problems of springBoot yml Files

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to summarize the list reading problems of springBoot yml files? in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

The problem of reading list from springBoot yml files

It's been going on for a long time. Record it.

The configuration is as follows # Custom data reporting information xx: # Group information machine1s:-name: XXXX frequency: null frequency-unit: null pressure: 0.01pressure-unit: Pa flow: 0 flow-unit: NM3/H state: downtime runtime: 17.5runtime-unit: days -name: XXXX frequency: 42.4 frequency-unit: HZ pressure: 0.39 pressure-unit: Pa flow: 4730 flow-unit: NM3/H state: run runtime: 12.5 runtime-unit: days-name: XXXX frequency: 46.4 frequency-unit: HZ pressure: 0.00 pressure-unit: Pa flow: 0 flow-unit: NM3/H state: downtime runtime: 8.2 runtime-unit: day-name: XXXX frequency: 41.4 frequency-unit: HZ pressure: 0.39 pressure-unit: Pa flow: 9532 flow-unit: NM3/ H state: run runtime: 3.2 runtime-unit: days-name: XXXX frequency: null frequency-unit: null pressure: 0.38 pressure-unit: Pa flow: 4800 flow-unit: NM3/H state: downtime runtime: 20.4 runtime-unit: days -name: XXXX frequency: null frequency-unit: null pressure: 0.01pressure-unit: Pa flow: 0 flow-unit: NM3/H state: stop runtime: 7.5runtime-unit: day 1. Define the configuration class @ ConfigurationProperties (prefix = "xx") public class TXMachinesProperties {private List machine1s; public List getMachine1s () {return machine1s;} public void setMachine1s (List machine1s) {this.machine1s = machine1s;}}

Note:

a. Here the prefix can be written to the previous level of the receiving object

b. The variable name here must be the same as the configured name before it can be automatically received.

two。 Define the configuration class for startup

This can actually be written with the above configuration class, but a class does one thing and does the isolation.

@ Configuration@EnableConfigurationProperties ({TXMachinesProperties.class}) public class TXMachinesConfig {} 3. Mode of use

You can use the following ways. Note here that since annotations using this yml are within the framework of SpringBoot, this attribute injection can only be used within the scope of classes annotated with Spring, not in normal classes.

@ Autowired private TXWorkShopAlarmProperties txWorkShopAlarmProperties

It can be solved.

Reading the list configuration YAML in the yml file supports the following data types

Object: a collection of key-value pairs, also known as mapping / hashes / dictionary (dictionary)

Array: a group of values arranged in order, also known as sequence / list

Scalar (scalars): a single, non-separable value

Only the reading of list type is introduced here.

List configuration in yml (lines starting with-form an array:):

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

Import jar package

Org.springframework.boot spring-boot-configuration-processor 2.2.1.RELEASE

Write an entity class configured by Wechat and map the value of each attribute configured in the configuration file to this entity class.

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 configs; @ Data public static class Config {private Integer schId; private String appId; private String appSecret; private String templateId;}} gets the contents of the configuration file

1. Inject the entity class

@ Autowired private WxConfig wxConfig

two。 Parsing, I am here to convert it to the map of the school id as key, which is convenient for me to use later. Just read the JSON.toJSONString (wxConfig) as you want.

Public Map getWeiXinConfig () {JSONObject o = JSON.parseObject (JSON.toJSONString (wxConfig)); JSONArray jsonArray = o.getJSONArray ("configs"); 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的输出结果:

This is the answer to the summary question on how to read springBoot yml files from list. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report