[Spring Boot study notes 03] Spring Boot configuration
Article Directory
1. Configuration file
1. Spring Boot configuration file
Spring Boot uses a global configuration file, the configuration file name is fixed, there are two kinds of properties and yaml ( yml ) files, the configuration file can be created by yourself.
•application.properties
• application.yml
Configuration file in src / main / resources directory or classpath / config under
2. Spring Boot configuration file function
Modify the default value of SpringBoot automatic configuration; SpringBoot automatically configures us at the bottom.
Two. YAML
1.yaml introduction
yaml is a file in YAML (YAML Ain't Markup Language) language. It is data -centric and more suitable for configuration files than json, xml, etc.
2.yaml syntax
2.1 Basic syntax
- Use indentation represent hierarchical relationship
-Tab key is not allowed when indenting, only spaces are allowed .
– The number of spaces for indentation is not important, as long as elements of the same level are aligned to the left
- Case Sensitive
k: (space) v: represents a pair of key-value pairs (spaces must be present);
Use space indentation to control the hierarchical relationship; as long as a column of data aligned to the left is at the same level
server:
port: 8081
path: /hello
Attributes and values are also case sensitive;
2.2 Data structure supported by yaml
– Object: a collection of key-value pairs
– Array: a set of values in order
– Literal value: a single, indivisible value
2.2.1 Object (key-value pair)
k: v: write the relationship between the properties and values of the object in the next line; pay attention to indentation
The object is still k: v
friends:
lastName: zhangsan
age: 20
Inline writing:
friends: {
lastName: zhangsan,age: 18}
2.2.2 Array (List, Set)
Use-value to represent an element in the array
pets:
- cat
- dog
- pig
Inline writing
pets: [cat,dog,pig]
2.2.3 Literals (numbers, strings, booleans, dates)
k: v: write directly literally;
Strings do not need single or double quotes by default;
"": Double quotation marks; special characters in the string will not be escaped ; special characters will be what they want to express
name: "zhangsan \n lisi": output; zhangsan \n lisi
'': Single quotation mark; special characters will be escaped , and special characters are ultimately just ordinary string data
name:'zhangsan \n lisi': output; zhangsan wraps lisi
Three. Configuration file value injection
1. The default utf-8 of the properties configuration file in IDEA may be garbled
The solution is as follows:
2. Comparison of @Value and @ConfigurationProperties for property injection value
@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;
Four.profile
Profile is Spring's support for different configuration functions for different environments. It can quickly switch environments by activating, specifying parameters, etc.
1. Multiple Profile files
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;
2. yml supports multiple document blocks
server:
port: 8081
spring:
profiles:
active: prod
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prod #指定属于哪个环境
3. Activate the specified profile
– Command line--spring.profiles.active=dev
-Configuration file spring.profiles.active=dev
– jvm参数 –Dspring.profiles.active=dev
1. Command line:
java -jar spring-boot-02-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
You can directly configure incoming command line parameters during testing
2. Specify spring.profiles.active=dev in the configuration file
3. Virtual machine parameters;
-Dspring.profiles.active=dev
Five. Configuration loading sequence
Spring Boot 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 above is in accordance withPriority from high to lowThe files in all positions will be loaded, and the high-priority configuration content will overwrite the low-priority configuration content . -We can also change the default configuration by configuring spring.config.location
We can also change the default configuration file location through spring.config.location
After the project is packaged, we can use command line parameters to specify the new location of the configuration file when starting the project; the specified configuration file and the configuration files loaded by default work together to form a complementary configuration;
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config.location=G:/application.properties
6. External configuration loading sequence
Spring Boot supports multiple external configuration methods
The priority of these methods is as follows:
- Command line parameters
- JNDI attributes from java:comp/env
- Java system properties (System.getProperties())
- Operating system environment variables
- Random.* property value configured by RandomValuePropertySource
- Application-{profile}.properties or application.yml (with spring.profile) configuration file outside the jar package
- The application-{profile}.properties or application.yml (with spring.profile) configuration file inside the jar package
- Application.properties or application.yml (without spring.profile) configuration file outside the jar package
- The application.properties or application.yml (without spring.profile) configuration file inside the jar package
- @PropertySource
ies or application.yml (with spring.profile) configuration file on the @Configuration annotation class - Application.properties or application.yml (without spring.profile) configuration file outside the jar package
- The application.properties or application.yml (without spring.profile) configuration file inside the jar package
- @PropertySource on @Configuration Annotated Class
- The default properties specified by SpringApplication.setDefaultProperties