1. Configuration File
Springboot uses a global configuration file with fixed configuration file names
- Application. Properties
- Application. yml
Roles of configuration files: Modify the default value of automatic spring boot configuration. Spring boot is automatically configured at the underlying layer;
Yaml(Yaml ain't Markup Language)
Yaml is the abbreviation of "yaml is not a markup language" [1] (see the original reference). However, to emphasize that this language is centered on data, instead of focusing on slogans, rename them with original words. It is an intuitive data serialization format that can be recognized by computers. It is a highly readable and easy to read by humans. It is easy to interact with scripting languages and is used to express data sequences.
Markup Language:
Most of the previous configuration files use the XXXX. xml file;
Yaml: data-centric, more suitable for configuration files than JSON and XML;
Yaml: configuration example
server: port: 8080
For XML:
<server> <port>8080</port></server>
Ii. yaml Syntax 1. Basic syntax
K: (Space) V: indicates a key-Value Pair (space is required)
Uses space indentation to control hierarchical relationships. As long as a column of data is left aligned, it is at the same level.
1 server:2 port: 80803 path: /hello
Attributes and values are case sensitive;
2. Value writing
Literal: Normal value (number, String, Boolean)
K: V: Directly write;
Strings are not added by default.Single quotesOrDouble quotation marks;
1) "": Double quotation marks: special characters in the string are not escaped.
2) ": single quotes: special characters are escaped. special characters are only common string data.
-
name: ‘sihai\n lisi‘
Output; Sihai \ n Lisi
Object, map(Attribute and value) (key-Value Pair)
K: V: Write the relationship between the attributes and values of the object in the next line. Pay attention to indentation. The object is still K: v.
Array(List, set)
Use-value to represent an element in the array
pets: - cat - dog - pig
In-line writing:
pets: [cat,dog,pig]
3. configuration file value Injection
Configuration File
Use in JavaBean:
/*** Map the values of each attribute configured in the configuration file to this component ** @ configurationproperties: tells springboot to bind all the attributes of this class to the configurations in the configuration file. ** prefix = "person ": all the attributes under the configuration file are mapped one by one. ** only the component in the container can provide the @ configurationproperties function; **/@ component @ configurationproperties (prefix = "user") public class user {private string name; private int age; private list <Object> lists; private pig ;}
In addition, we can import the configuration file processor, and you will be prompted to write the configuration later.
<! -- Import the configuration file processor and bind the configuration file with a prompt --> <dependency> <groupid> org. springframework. boot </groupid> <artifactid> spring-boot-configuration-processor </artifactid> <optional> true </Optional> </dependency>
1. The properties configuration file is fixed by UTF-8 garbled by default in idea.
2. @ value get value and @ configurationproperties get Value Comparison
@ Configurationproperties |
@ Value |
Function |
Batch inject properties in the configuration file |
Loose binding (loose syntax) |
Supported |
Spel |
Not Supported |
Jsr303 Data Validation |
Supported |
Complex Type Encapsulation |
Not Supported |
Configuration file details