The configuration file
SpringBoot uses a global configuration file with a fixed name
-
application.properties
Syntax structure: key=value
server.port=8081 Copy the code
-
application.yml
Syntax structure: key: space value
server: port: 8081 Copy the code
What the configuration file does: Change the default values for SpringBoot autoconfiguration, because SpringBoot automatically configures us underneath.
1. What is YAML
YAML is a recursive abbreviation of “YAML Ain’t a Markup Language” (YAML is not a Markup Language). When the Language was developed, YAML actually meant: “Yet Another Markup Language”
This is a data-centric language, not a markup language!
Yaml basic syntax
Strict grammar requirements!
1, the space can not be omitted
2. Control hierarchy by indentation, so that all columns aligned to the left are in the same hierarchy.
3. The case of attributes and values is very sensitive.
server:
port: 8081
# Normal key-value pairs
name: Wan Li
In the next line, write the properties and values of the object. Note the indentation
student:
name1: wanli
age:3
# Object inline writing
student1: {name: wanli.age: 3}
An array uses a -value to represent an element in the array
pets:
- cat
- dog
- pig
# Inline writing
pets1: [cat.dog.pig]
Copy the code
Note:
-
The “” double quotation marks do not escape the special characters in the string. The special characters are used as their intended meanings.
For example: name: “cheng\n shen” output: cheng newline shen
-
“Single quote, special characters are escaped, and will eventually be printed as normal characters
Example: name: ‘cheng\n shen’ Output: cheng\n shen
3. Inject yamL files
The yamL file is even more powerful because it can inject matches directly into our entity classes!
Write two entity classes
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
private String name;
private Integer age;
}
Copy the code
@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
Copy the code
Using the YAML configuration, create a new file application.yml in the Resources directory of the SpringBoot project
person:
name: wanli
age: 3
happy: true
birthday: 2021/ 4/25
maps: {k1: v1.k2: v2}
lists:
- code
- music
- game
dog:
name: Liu's dog
age: 3
Copy the code
Now that we’ve written all the values of the person object, let’s inject them into our class!
/* @configurationProperties: Map the value of each property configured in the configuration file to the component; Tell SpringBoot to bind all properties in this class to the relevant configuration in the configuration file. The parameter prefix = "person" : matches all properties under person in the configuration file to */
@Component / / registered bean
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean happy;
private Date birthday;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
}
Copy the code
Note: With @ConfigurationProperties, the IDEA will have a hint at the top and add the following dependencies to resolve the problem
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Copy the code
Test under SpringBoot’s test class
package com.cheng;
import com.cheng.pojo.Dog;
import com.cheng.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot02ConfigApplicationTests {
@Autowired
private Person person;
@Test
void contextLoads(a) { System.out.println(person); }}Copy the code
Test results, OK!
Yaml supports loose binding: what does this mean? For example, last-name in my YML, which is the same as lastName, followed by – is uppercase by default. This is called loose binding.