2. yaml configuration injection
2.1 configuration file
SpringBoot uses a global configuration file with a fixed name
-
application.properties
-
- Syntax structure: key=value
-
application.yml
-
- Syntax structure: key: space value
- Note: when configuring yaml, a space must be strictly left after the ":"
Function of configuration file: modify the default value of SpringBoot automatic configuration, because SpringBoot is automatically configured at the bottom;
For example, we can modify the default startup port number of Tomcat (8080) in the configuration file
server.port=8081
2.2 yaml overview and syntax
1. General
YAML is a recursive abbreviation of "YAML Ain't a Markup Language". When developing this language, YAML actually means "Yet Another Markup Language"
This language is data centric, not markup language focused!
Most of the previous configuration files were configured using xml
Traditional xml configuration:
<server> <port>8081<port> </server>
yaml configuration:
server: prot: 8080
2. Basic grammar
Note: strict grammar requirements!
1. Spaces cannot be omitted
2. Indentation is used to control the hierarchical relationship. As long as a column of data aligned on the left is at the same level.
3. Attributes and values are case sensitive.
Literal: normal value [number, Boolean, string]
The literal quantity can be written directly after the string. By default, double quotation marks or single quotation marks are not added to the string;
k: v
be careful:
-
"" double quotation marks will not escape the special characters in the string, and the special characters will be output as the meaning they want to express
For example: name: "kuang \n shen" output: kuang newline shen
-
'' single quotation mark, which will escape special characters, and the special characters will eventually be output like ordinary characters
For example: name: 'kuang \n shen' output: kuang \n shen
Object, Map (key value pair)
#Object, Map format k: v1: v2:
Write the attribute and value relationship of the object in the next line, and pay attention to indentation; for example:
student: name: qinjiang age: 3
Inline writing
student: {name: qinjiang,age: 3}
Array (List, set)
Use the - value to represent an element in the array, for example:
pets: - cat - dog - pig
Inline writing
pets: [cat,dog,pig]
Modify the default port number of SpringBoot
Add the parameter of port number in the configuration file to switch ports;
server: port: 8081
2.3 yaml injection profile
1. Create a new file application.yaml in the resources directory of the springboot project
2. Write two entity classes to test
@Component //Register bean s into the container public class Dog { private String firstname; private Integer age; // Parameterless construction, get, set method, toString() method }
@Component //Register bean s into the container public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; // Parameterless construction, get, set method, toString() method }
3. Inject using yaml configuration, and configure in the previously created application.yaml file!
Person: name: aadzj@qq.com age: 18 happy: true birth: 2021/09/22 maps: {k1: v1,k2: v2} hello: hi lists: -code -music -girl dog: name: Wangcai age: 3 dog: first-name: Xiao Huang age: 5
4. Inject the data configured in the application.yaml file into the entity class
How to implement injection? Add the following annotation to the previous entity class: @ ConfigurationProperties(prefix = "xxx")
/* @ConfigurationProperties effect: Map the value of each attribute configured in the configuration file to this component; Tell SpringBoot to bind all properties in this class to the relevant configuration in the configuration file Parameter prefix = "person": match all attributes under person in the configuration file one by one */ @Component //Register bean s into the container @ConfigurationProperties(prefix = "person") //Key notes for successful injection public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; // Parameterless construction, get, set method, toString() method }
5. At this time, the IDEA prompts that the springboot configuration annotation processor is not found. Let's look at the document. We can look at the document and find a dependency!
6. Add the configuration file processor dependency in the pom.xml file: spring boot configuration processor
<!-- After importing the configuration file processor, you will be prompted to restart the configuration file binding --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
In this way, the data in the application.yaml file is injected into the entity class
If the key value of the configuration file is different from the name of the entity class attribute, the result output is null and the attribute value injection fails
2.4 loading the specified configuration file
@PropertySource: load the specified configuration file;
@configurationProperties: get the value from the global configuration file by default;
1. Let's create a new person.properties file in the resources directory
name=kuangshen
2. Then specify in our code to load the person.properties file
@PropertySource(value = "classpath:person.properties") @Component //Register bean public class Person { private String name; private Integer age; private Boolean happy; ...... }
2.5 placeholder for configuration file
person: name: aadzj@qq.com${random.uuid} # Random uuid age: ${random.int} # Random int happy: true birht: 2021/09/22 maps: {k1: v1,k2: v2} hello: hi lists: -code -music -girl dog: name: ${Person.hello:hello}_Wangcai #Use placeholder mode age: 3 dog: first-name: Xiao Huang age: 5
2.6 conclusion
[note] when writing the properties configuration file in Chinese, there will be garbled codes. We need to set the encoding format to UTF-8 in the IDEA;
Settings -- > configuration in file encodings
Values can be obtained by configuring yml and configuring properties. yaml is strongly recommended
If we only need to obtain a value in the configuration file in a business, we can use @ value;
If we specially write a JavaBean to map one by one with the configuration file, we can directly @ configurationProperties. Don't hesitate!