<> Profile classification

SpringBoot In addition to support properties Configuration file in format , Two other formats of configuration files are also supported . They are as follows :

* properties format
* yml format
* yaml format
give an example :

* application.properties(properties format ) server.port=80
* application.yml(yml format ) server: port: 81
* application.yaml(yaml format ) server: port: 82
yml Format and yaml The format is different except for the file name suffix , The format is exactly the same

<> summary

SpringBoot Provided 3 Formats of configuration files

* properties( Traditional format / default format )
* yml( Mainstream format )
* yaml
<> Profile priority

give an example :

* application.properties(properties format ) server.port=80
* application.yml(yml format ) server: port: 81
* application.yaml(yaml format ) server: port: 82
​ After startup, it is found that the current startup port is 80, hold 80 Delete the corresponding file , Then start , Now the port is changed to 81. Now we know 3 What is the loading priority of files
application.properties > application.yml > application.yaml
​ Finally, let's modify the content of the configuration file

* application.properties(properties format ) server.port=80
spring.main.banner-mode=off
* application.yml(yml format ) server: port: 81 logging: level: root: debug
* application.yaml(yaml format ) server: port: 82

We found that not only ports are in effect , Final display 80, At the same time, the other two configurations are also effective , It seems that the items in each configuration file will take effect , However, if there is the same type of configuration in multiple configuration files, the configuration in the file with higher priority will overwrite the configuration in the file with higher priority . If the configuration items are different , Then all configuration items will take effect .

summary

* Load priority between profiles properties( highest )> yml > yaml( minimum )
* The same configuration in different configuration files overwrites each other according to the loading priority , Different configurations in different configuration files are reserved
<>yaml file

YAML(YAML Ain’t Markup
Language), A data serialization format . Easy to read , Easy interaction with scripting language , Data as the core , Data over format . There are two common file extensions :

*
.yml format ( main stream )

*
.yaml format

For the document itself, when writing , It has strict grammatical format requirements , The details are as follows :

* Case sensitive
* Attribute hierarchy is described in multiple lines , End each line with a colon
* Use indentation to represent hierarchical relationships , Align left side of the same level , Only spaces are allowed ( Not allowed Tab key )
* Add a space before the attribute value ( Use colon between attribute name and attribute value + Space as separation )
* # number Indicates a comment
A core rule to remember , Data should be separated by spaces and colons

​ Common data writing formats are listed below
boolean: TRUE #TRUE,true,True,FALSE,false,False All right float: 3.14 #6.8523015e+5
# Support scientific counting int: 123 #0b1010_0111_0100_1010_1110 # Binary support , octal number system , hexadecimal null: ~ # use ~ express null
string: HelloWorld # Strings can be written directly string2: "Hello World" # Special characters can be wrapped in double quotation marks date: 2018-
02-17 # Date must be used yyyy-MM-dd format datetime: 2018-02-17T15:02:31+08:00 # Use between time and date T connect , Last use
+ Represents time zone
in addition ,yaml Arrays can also be represented in the format , Use the minus sign below the writing position of the attribute name as the data start symbol , Write one data per line , The minus sign is separated from the space between the data
subject: - Java - front end - big data enterprise: name: itcast age: 16 subject: - Java -
front end - big data likes: [ Glory of Kings , Stimulate the battlefield ] # Array writing abbreviation format users: # Object array format I - name: Tom age: 4 - name:
Jerry age: 5 users: # Object array format II - name: Tom age: 4 - name: Jerry age: 5 users2: [ {
name:Tom , age:4 } , { name:Jerry , age:5 } ] # Object array abbreviated format
summary

* yaml rule of grammar
* Case sensitive
* Attribute hierarchy is described in multiple lines , End each line with a colon
* Use indentation to represent hierarchical relationships , Align left side of the same level , Only spaces are allowed ( Not allowed Tab key )
* Add a space before the attribute value ( Use colon between attribute name and attribute value + Space as separation )
* # number Indicates a comment
* Note that there is a space between the colon of the attribute name and the data
* Literal , Object data format , Array data format
<>yaml data fetch

about yaml Data in file , In fact, you can imagine that this is a small database , There are several data stored in it , Each data has an independent name , If you want to read the data inside , It must be supported , Here is the introduction 3 A way to read data

<> Read single data

​ yaml Single data saved in , have access to Spring Direct reading of annotations in , use @Value Single data can be read , Attribute name reference method :${ First level attribute name . Secondary attribute name ……}

Remember to use @Value When commenting , To write the injection in a specified Spring Regulated bean Above the attribute name of . Now you can read the corresponding single data row
summary

* use @Value coordination SpEL Read single data
* If there are multiple levels of data , Write the name of the level in sequence
<> Read all data


SpringBoot Provides an object , It can encapsulate all data into one object , This object is called Environment, Using auto assembly annotation, all yaml Data is encapsulated into this object


The data is encapsulated in Environment Object , When getting properties , adopt Environment Interface operation of , Specific methods getProperties(String), Fill in the attribute name for the parameter

summary

* use Environment Object encapsulates all configuration information
* use @Autowired Auto assemble data to Environment Object
<> Read object data

First define an object , And include the object Spring Scope of control , That is, defined as a bean Make it in the container , Then use annotations @ConfigurationProperties Specify which group this object loads yaml Information configured in .


this @ConfigurationProperties You must tell him what the prefix of the loaded data is , In this way, all attributes under the current prefix are encapsulated into this object . Remember that the data attribute name should correspond to the variable name of the object one by one , Otherwise, it can't be encapsulated . In fact, if you want to define a set of data in the future, you can use it yourself , You can write an object first , Then define the attributes , Next, write according to this format in the configuration .

summary

* use @ConfigurationProperties Annotation binding configuration information to the encapsulated class
* The encapsulation class needs to be defined as Spring Managed bean, Otherwise, attribute injection cannot be performed

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?