content
1. File Type
1.1、properties
properties file is a configuration file, mainly used to store configuration information, etc.
1.2、yaml
1.2.1. Introduction
YAML is a recursive acronym for "YAML Ain't Markup Language" (YAML is not a markup language). When this language was developed, YAML actually meant: "Yet Another Markup Language" (still a markup language).
Great for data-centric profiles
1.2.2. Basic syntax
-
key: space between value kv (space after colon)
-
Case Sensitive
-
Use indentation to indicate hierarchy
-
Indentation does not allow tabs, only spaces
-
The number of spaces indented does not matter, as long as elements at the same level are left-aligned
-
'#' means a comment
-
The string does not need to be quoted. If you want to add it, '' and "" indicate that the content of the string will be escaped/not escaped
1.2.3, data type
-
Literal: A single, indivisible value. date, boolean, string, number, null
String type can be represented by single or double quotes
# Single quotes will output \n as a string, double quotes will output \n as a newline
# Single quotes will not make the escape character effective, double quotation marks will make the escape character effective
k: v
-
Object: A collection of key-value pairs. map, hash, set, object
#行内写法:
k: {k1:v1,k2:v2,k3:v3}
#或(注意缩进对齐 和 空格)
k:
k1: v1
k2: v2
k3: v3
-
Array: An ordered set of values. array, list, queue
#行内写法:(注意空格)
k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
1.2.4. Examples
① Introduce lombok dependencies and create two classes
@Component
@ConfigurationProperties(prefix = "person")
@Data
@ToString
public class Person {
private String userName;
private Boolean boss;
private Date birth;
private Integer age;
private Pet pet;
private String[] interests;
private List<String> animal;
private Map<String, Object> score;
private Set<Double> salarys;
private Map<String, List<Pet>> allPets;
}
@Data
@ToString
public class Pet {
private String name;
private Double weight;
}
② Create a yaml configuration file, application.yaml, the suffix can also be yml
person:
userName: zhangsan
boss: false
birth: 2019/12/12 20:12:33
age: 18
pet:
name: tomcat
weight: 23.4
interests: [ 篮球,游泳 ]
animal:
- jerry
- mario
score:
english:
first: 30
second: 40
third: 50
math: [ 131,140,148 ]
chinese: { first: 128,second: 136 }
salarys: [ 3999,4999.98,5999.99 ]
allPets:
sick:
- { name: tom }
- { name: jerry,weight: 47 }
health: [ { name: mario,weight: 47 } ]
2. Configuration Tips
Custom class and configuration file bindings are generally silent. And it will prompt that the SpringBoot configuration annotation processor is not configured
Just add dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
To avoid packaging annotation processors together
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>