The mad God said:
https://mp.weixin.qq.com/s__biz=Mzg2NTAzMTExNg==&mid=2247483744&idx=1&sn=b4ec762e71b2ddf9403c035635299206&scene=19#wechat_redirect
1. Configuration file
SpringBoot uses a global configuration file. The name of the configuration file is fixed application. The configuration file can have two formats
-
application.properties
Syntax structure: key=value -
application.yml
Syntax structure: key: space value
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 in the configuration file!
server.port=8081
2. yaml overview
YAML is a recursive abbreviation for "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; For example, a simple port configuration, let's compare yaml and xml
-
Traditional xml configuration:
<server> <port>8081<port> </server>
-
yaml configuration:
server: prot: 8080
3. yaml basic grammar
Note: strict grammar requirements!
-
Spaces cannot be omitted
-
Indentation is used to control the hierarchical relationship. As long as a column of data aligned on the left is at the same level.
-
Attributes and values are case sensitive.
Syntax:
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 used as the meaning they want to express;
For example: name: "kuang \n shen" output: kuang newline shen
'' single quotation mark will escape special characters, and the special characters will eventually be output as 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 the 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: 8082
4. yaml injection - inject matching values into entity classes
yaml file is more powerful because it can directly inject matching values into our entity classes!
-
Create a new file application.yml in the resources directory of the springboot project
-
Write an entity class Dog;
package com.kuang.springboot.pojo; @Component //Register bean s into the container public class Dog { private String name; private Integer age; //Parameterless construction, get, set method, toString() method }
-
Writing a more complex entity class: Person class, including dog
@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 }
-
Write a yaml configuration! Pay attention to indentation and case
person: name: qinjiang age: 3 happy: false birth: 2000/01/01 maps: {k1: v1,k2: v2} lists: - code - girl - music dog: name: Wangcai age: 1
-
Inject into class!
@ConfigurationProperties function: (get the value from the global configuration file by default)
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 @ConfigurationProperties(prefix = "person") 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; }
-
The IDEA will prompt that the springboot configuration annotation processor is not found, so the corresponding jar package is introduced
<!-- 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>
-
The above configuration is completed. You can test and output Person
@SpringBootTest class DemoApplicationTests { @Autowired Person person; //Inject person automatically @Test public void contextLoads() { System.out.println(person); //Print person information } }
Profile placeholder
Configuration files can also write placeholders to generate random numbers
person: name: qinjiang${random.uuid} # Random uuid age: ${random.int} # Random int happy: false birth: 2000/01/01 maps: {k1: v1,k2: v2} lists: - code - girl - music dog: name: ${person.hello:other}_Wangcai age: 1