1. Basic grammar and expression of yml
1. Basic grammar of yaml
1. k: (space) v represents a key-value pair
key: value
2. Use indentation to indicate hierarchical relationships
key:
child-key1: value1
child-key2: value2
3. Attributes and values are case sensitive
4. Comments
Use # for comments
#xxxxxxx
key: value
2. The expression of value
1. Object
Student:
name: tom
age: 11
#行内写法
Student: {
name:tom,age:11}
2. Array
nums:
- 1
- 2
- 3
#行内写法
nums: {
1,2,3}
Two, springboot configuration file
You can import the configuration file processor before writing, so that you will be prompted when writing yml
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
1. Injection of configuration file values
Prerequisite: The javabean corresponding to the configuration file must be placed in the IOC container, that is, using @Component annotation
Method 1
uses @ConfigurationProperties(prefix = "person") to set a value for binding, and set the properties of the class through this binding value in the configuration file.
javabean
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date date;
private Map<String,Object> maps;
private List<Object> list;
private Dog dog;
....
}
@Component
public class Dog {
private String name;
private Integer age;
...
}
yml configuration file
person:
lastname: zhangsan
age: 11
boss: true
date: 2010/11/11
maps: {
k1: v1,k2: v2}
list:
- list1
- list2
- list3
dog:
name: xiaoming
age: 2
result:
Person{lastName='zhangsan', age=11, boss=true, date=Thu Nov 11 00:00:00 CST 2010, maps={k1=v1, k2=v2, kw=v2}, list=[list1, list2, list3], dog=Dog{name='xiaoming', age=2}}
Method 2:
You can assign the value to the object by using @PropertySource to specify the properties configuration file
Configuration file
person.last-name=张三
person.age=11
person.boss=false
person.date=2021/2/2
person.maps.k1=v1
person.maps.kw=v2
person.list=a,x,c,v
person.dog.name = dog
person.dog.age = 15
javabean
/**
* 将配置文件中的每一个属性的值映射到这个组件中
* @ConfigurationProperties 高数springboot将本类中所有属性和配置文件中相关的配置进行绑定
* prefix 在配置文件中哪个下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才能提供@ConfigurationProperties的功能
*
* @PropertySource 导入spring的配置文件,让配置文件中的内容生效
*/
@PropertySource(value = {
"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date date;
private Map<String,Object> maps;
private List<Object> list;
private Dog dog;
...
}
result
Person{lastName='张三', age=11, boss=false, date=Tue Feb 02 00:00:00 CST 2021, maps={k1=v1, kw=v2}, list=[a, x, c, v], dog=Dog{name='dog', age=15}}
Method 3
Use @Value annotation to assign value to attribute
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
@Value("zhangsan")
private String lastName;
@Value("11")
private Integer age;
private boolean boss;
private Date date;
private Map<String,Object> maps;
private List<Object> list;
private Dog dog;
result
Person{lastName='zhangsan', age=11, boss=false, date=null, maps=null, list=null, dog=null}
2, @Value get value and @ConfigurationProperties get value comparison
@ConfigurationProperties | @Value | |
---|---|---|
Features | Bulk injection of properties in configuration files | Specify one by one |
Loosely bound (loose syntax) | stand by | not support |
Game | not support | stand by |
JSR303 data verification | stand by | not support |
Complex type package | stand by | not support |
Both the configuration file yml and properties can get the value;
If we just need to get a value in the configuration file in a certain business logic, use @Value;
If we say that we have specially written a javaBean to map with the configuration file, we will directly use @ConfigurationProperties;
3. Configuration file injection data verification
Before using verification, you need to import the dependency of field verification
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
First use the @Validated annotation to decorate the class to be verified, and then use the following annotations to decorate the properties to be verified in the class
@AssertFalse 校验false
@AssertTrue 校验true
@Email 校验是否是邮箱格式
@DecimalMax(value=,inclusive=) 小于等于value,
inclusive=true,是小于等于
@DecimalMin(value=,inclusive=) 与上类似
@Max(value=) 小于等于value
@Min(value=) 大于等于value
@NotNull 检查Null
@Past 检查日期
@Pattern(regex=,flag=) 正则
@Size(min=, max=) 字符串,集合,map限制大小
@Validate 对po实体类进行校验
Use mode three configuration file injection
@PropertySource(value = {
"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
// @Value("zhangsan")
@Email
private String lastName;
// @Value("11")
private Integer age;
private boolean boss;
private Date date;
private Map<String,Object> maps;
private List<Object> list;
private Dog dog;
...
}
result:
java.lang.IllegalStateException: Failed to load ApplicationContext
4. Configuration file placeholder
#配置person的值
#配置文件的占位符${},如果想设没有的默认值可以用${name:defualt}
#通过使用${random.xxxx}随机数 例${random.value}、${random.int}、${random.long}、${random.int(10)}、${random.int[1024,65536]}等等
person.last-name=张三${random.int}
person.age=11
person.boss=false
person.date=2021/2/2
person.maps.k1=v1
person.maps.kw=v2
person.list=a,x,c,v
person.dog.name = ${person.hello:hello}dog
person.dog.age = 15
result
//结果1
Person{lastName='张三-1155973093', age=11, boss=false, date=Tue Feb 02 00:00:00 CST 2021, maps={k1=v1, kw=v2}, list=[a, x, c, v], dog=Dog{name='hellodog', age=15}}
//结果2
Person{lastName='张三1191295749', age=11, boss=false, date=Tue Feb 02 00:00:00 CST 2021, maps={k1=v1, kw=v2}, list=[a, x, c, v], dog=Dog{name='hellodog', age=15}}
5. Multiple Profile files
1. Multi-Profile file When
we write the main configuration file, the file name can be application-{profile}.properties/yml
. The configuration of
application.properties is used by default; application.properties file
#激活使用application-dev.properties配置文件
spring.profiles.active=dev
application-dev.properties file
#使用application-{profile},properties/yml取配置文件名
#默认使用的是application.properties
server.port=8081
application-prod.properties file
server.port=8082
result
2. yml supports multiple document blocks
#yml支持多文档块方式用 --- 来分割文档块
server:
port: 8080
spring:
profiles:
active: prod
---
server:
port: 8081
spring:
profiles: dev #制定属于那个环境
---
server:
port: 8082
spring:
profiles: prod
---
result
6. Configuration file loading location
Springboot startup will scan the application.properties or application.yml file in the following location as the default configuration file for Spring boot
file:./config/
file:./
classpath:/config/
classpath:/
The priority is from high to low, and the configuration with high priority will override the configuration with low priority;
SpringBoot will load the main configuration file from these four locations; complementary configuration ;
You can also change the default configuration file location through spring.config.location
7. External configuration loading sequence
SpringBoot can also load the configuration from the following locations; priority from high to low; high priority configuration overrides low priority configuration, all configurations will form complementary configurations
1. Command line parameters
All configurations can be specified on the command line
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc
Multiple configurations are separated by spaces; --configuration item=value
2. JNDI attributes from java:comp/env
3. Java system properties (System.getProperties())
4. Operating system environment variables
5. Random.* property value configured by RandomValuePropertySource
Search from the jar package to the jar package;
Load with profile first
6. Application-{profile}.properties or application.yml (with spring.profile) configuration file outside the jar package
7. Application-{profile}.properties or application.yml (with spring.profile) configuration file inside the jar package
Load again without profile
8. Application.properties or application.yml (without spring.profile) configuration file outside the jar package
9. Application.properties or application.yml (without spring.profile) configuration file inside the jar package
10.@PropertySource on @Configuration Annotated Class
11. The default properties specified by SpringApplication.setDefaultProperties
All supported configuration loading sources;