How to write the yaml configuration file
Springboot provides a global configuration file, the file name must be application, and the format can be properties and yaml\yam. The official recommendation is that we use yaml or yam. There are
two suitable differences:
Properties file storage method:
key=value
yaml method storage:
key: space value
Syntax of yaml format file:
yaml stores a single value (string, numeric, boolean, etc.)
key: value
注意:一定要注意格式,中间会有一个空格。
双引号不会转义特殊字符 \n就代表了换行
单引号会自动转义特殊字符,会把特殊字符作为字符串输出
yaml stores map format\object format data
key:
key1: v1
key2: v2
例子:map
maps:
haha: 13
lala: 24
对象:
person:
name: 张三
age: 12
可以转换为行内写法
key: {
key1: v1,key2: v2}
例如:
person: {
name: 张三,age: 18}
注意格式写法,不要把空格忘了
yaml array format data:
格式:
key:
- v1
- v2
- v3
- ...
例子
lists:
- haha
- lala
- papa
行内写法:
key: [v1,v2,v3]
例子:
lists: [haha,lala,papa]
2. The use of yaml
General configuration files are used to assign values to properties in javabean. After the properties file is loaded (you can also load the specified file with @PropertiesSource(value="classpath:xxx.properties")), we can use ${key} To get the value of value, our yaml is not so troublesome.
1. In the resources directory of the project, create an application.yaml file (it will be automatically recognized by springBoot)
2. Create a pojo package and create two entity classes for testing:
3. Configure in the application.yaml file:
springBoot It will automatically load the properties or yaml file at the beginning of the application, and then use the @ConfigurationProperties(prefix = "person") annotation to match the person in the yaml file, and finally assign values to the properties through the set method of the entity class:
this way we can It is convenient to assign values to attributes.
3. Solve the problem of red warning when using annotation to bind yanl file
After binding, there will always be this red on the entity class, we can import a dependency, and then restart it to disappear
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
4.yaml placeholder
person:
name: qinjiang${
random.uuid} # 随机uuid
age: ${
random.int} # 随机int
happy: false
birth: 2000/01/01
maps: {
k1: v1,k2: v2}
lists:
- code
- girl
- music
dog:
#如果person.hello存在,就是用person.hello的值,不存在使用other
name: ${
person.hello:other}_旺财
age: 1
5 Conclusion:
Conclusion excerpted from: Mad God says java
# Multi-environment switching and configuration file priority## 1. Multi-environment switching 1.yaml method: ![insert picture description here](https://img-blog.csdnimg.cn/20210116160053845.png?x-oss -process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzQzMTEyMw==,size_16,color_FFFFFF,t_70)
The one above is used by default. When we want to use other environments, we can add in the default:
2. Properties file switching environment
Only need to create a default configuration file
application.properties.
Other environments can be: application-xxx.properties.
When we want to switch, add code in the application.properties file
spring.profiles.active=xxx
Can replace
Storage location and priority of configuration files
: The configuration file can be stored in
case the project directory under config directory with the
project root directory
under the config directory under the CLASSPATH (Resources)
the CLASSPATH under
When the configuration file names are the same, the priority is:
config under the project directory "root directory of the project" config directory under the classpath "under the classpath