YML explanation - springboot

catalogue

preface

Definition of YML

Syntax introduction of YML (YAML)

Basic grammar

Translation character

About the name of the key

About placeholders

springboot uses the yml variable

@Value usage

@ConfigurationProperties use

 

preface

Usually, our springboot project often uses YML configuration files. After using them for a period of time, I suddenly found that I didn't know so much about the various uses of YML files. So I wrote this article today to explain my current understanding of YML file configuration for recording. Of course, with more knowledge of YML, this article will continue to be revised.

 

Definition of YML

First, let's look at the definition of YML. YML(YAML): YAML Ain't Markup Language. It makes it clear that YML is not a markup language. It has a good data structure for programmers to read.

 

Syntax introduction of YML (YAML)

Basic grammar

Let's first look at a typical json data, and then we implement it through YML. In this way, let's understand the syntax of YML.

We want to describe a user. The user has name and age. The contact information is a bean, then the hobby is a list, and the friend is in the form of a list bean. json format is as follows:

{
	"user-name": "zhangsan",
	"age": 18,
	"contact": {
		"mobile": "11111111111",
		"email": "a@a.com"
	},
	"hobbies": [
		"football", "basketball"
	],
	"friends": [{
			"friend-name": "lisi",
			"gender": "male"
		},
		{
			"friend-name": "wangwu",
			"gender": "femle"
		}
	]
}

Then, if we are specifically for YML, it is expressed as follows:

user:
  user-name: zhangsan
    age: 18
    contact:
      mobile: 11111111111
      email: a@a.com
    hobby: 
      - football
      - basketball
    friends: 
      - friend-name: lisi
        gender: male
      - firend-name: wangwu
        gender: female

In the syntax of YML, there must be a space after ":, and then there must be two spaces in front of the attribute of the next level, and it must be checked at the same level.

ren is then the internal attribute of friends. There is another way to write it.

friend:
  - {friend-name: lisi, gender: male}
  - {friend-name: wangwu, gender: female}

Translation character

See the following figure to see the function of translated characters

About the name of the key

In springboot, friend name = friendname = friend_ name. In this regard, when I got up early to use the springboot project, I didn't know that yml was like this. As a result, when I looked at many documents to configure the properties of yml, I found that some configurations were with "-" and some were humped. I didn't understand how to write correctly-_-||

 

About placeholders

When using the springboot configuration file, we often have different parameters due to different running environments. We can change the content of the configuration through the placeholder of the yml file. For example: user name: ${other.name}. Then we can also write: user name: ${other.name: zhangsan}, and give a default value after ":. Represents if other If there is no name attribute, it defaults to zhangsan.

yml also has a placeholder for random numbers. Let me just give a few simple examples.

For example, age: ${random.int}, which is written as a random integer for age.

Use of other random:

${random.uuid}: get a uuid

${random.long}: get a long integer random number

${random.int(100)}: get a random integer within 100

${random. Int (50100)}: get a random integer from 50 to 100

 

springboot uses the yml variable

@Value usage

@Component
@Data
public class User{
  @Value("${user.user-name}")
  private String userName;
}

@ConfigurationProperties use

@Data
@Component
@ConfigurationProperties(prefix="user")
public class User{

  private String userName;
  private int age;
  private String[] hobbies;
  private Contact contact;
  private Friend[] friends;
}

@Data
public class Contact{
  private String mobile;
  private String email;
}

@Data
public class Friend{
  private String friendName;
  private String gender;
}

 

Comparison of two binding variables
describe @Value @ConfigurationProperties
function Single formulation injection Batch inject a java bean
Syntax binding Loose syntax is not supported Support loose syntax
Objects and arrays I won't support it support
SpEL support I won't support it

 

Tags: Spring Boot yaml

Posted by phpuser_2000 on Sat, 21 May 2022 23:52:33 +0300