Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference in the way arrays are written in springBoot configuration files properties and yml

Shulou Source: shulou.com Published: 2022-06-02 09:49:46 10月12日 Update

This article introduces the knowledge about "springBoot configuration file properties and array writing in yml are different". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to handle these situations! I hope you can read carefully and learn something!

SpringBoot configuration file properties and yml array writing

Here we introduce the use of arrays in the two file configuration methods in springBoot, that is, collections.

Here are the application.properties files I use in springBoot

It's actually easy to understand, my configs is a collection, configs[0].appid represents the value of appid in the first object I configured

miniapp.configs[0].appid = 111111miniapp.configs[0].secret= 222222miniapp.configs[0].token = 333333miniapp.configs[0].aesKey = 444444miniapp.configs[0].msgDataFormat = JSON miniapp.configs[1].appid = 111miniapp.configs[1].secret = 222miniapp.configs[1].token = 333miniapp.configs[1].aesKey = 444miniapp.configs[1].msgDataFormat = JSON

This is the way to use application.yml. Because YAML itself supports list types, you can add:

Yml If you configure a normal string

miniapp: configs: - appid: 111 secret: 222 token: 333 aesKey: 444 msgDataFormat: JSON - appid: 111 secret: 222 token: 333 aesKey: 444 msgDataFormat: JSON You can choose either of these two methods

Here is how the class code is written:

package com.platform.miniprogram; import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component; import java.util.List; /** * @Classname WxMaProperties * @Description TODO * @Date 2020/10/10 10:48 * @Created by lyc */@Data@ConfigurationProperties(prefix = "miniapp")@Componentpublic class WxMaProperties { private List configs; @Data public static class Config { /** * Set appid of Weixin Mini Programs */ private String appid; /** * Set Secret of Weixin Mini Programs */ private String secret; /** * Set the token configured by Weixin Mini Programs (Mini) message server */ private String token; /** * Set EncodingAESKey configured by Weixin Mini Programs (Mini) message server */ private String aesKey; /** * Message format, XML or JSON */ private String msgDataFormat; } }

Explanation:

@Data is omitting the get/set method. You can delete it and write it as get/set.

@ConfigurationProperties(prefix = "miniapp")

prefix This prefix must be written

configs is the name of the collection, consistent with the information in the configuration table. That's pretty much it.

Difference between.properties and.yml

For example: redis configuration properties or yml file, as follows:

spring.redis.cluster.nodes[0]=192.168.0.1:6379 spring.redis.cluster.nodes[1]=192.168.0.2:6379

or

spring: redis: cluster: nodes: - 192.168.0.1:6379 - 192.168.0.2:6379

Example: 2

environments: dev: url: http://dev.bar.com name: Developer Setup prod: url: http://foo.bar.com name: My Cool App

The above YAML document is converted to the following properties:

environments.dev.url=http://dev.bar.comenvironments.dev.name=Developer Setupenvironments.prod.url=http://foo.bar.comenvironments.prod.name=My Cool App

YAML lists are represented using [index] indirect references as attribute keys, such as the following YAML:

my: servers: - dev.bar.com - foo.bar.com

This will translate into the following attributes:

my.servers[0]=dev.bar.commy.servers[1]= foo.bar.com "springBoot configuration file properties and yml array writing what is different" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Configuration file writing array program facet property message different content way method more server knowledge service practical ordinary consistent learned Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi Apple Shulou Technology macOS