[SpringBoot configuration file] Detailed explanation of yaml & properties

SpringBoot configuration file 

SpringBoot uses a global configuration file with a fixed name: application.properties/application.yml

Location: generally placed in the src/main/resources directory or classpath/config 

Function: When spring boot automatic configuration cannot meet the requirements, some default configuration values ​​can be modified (that is, the default value of SpringBoot automatic configuration can be modified)

Introduction to yaml

YAML is a recursive abbreviation for "YAML Ain't a Markup Language" (YAML is not a markup language). When this language was developed, YAML actually meant "Yet Another Markup Language" (another markup language).
But later the maintainers of the project renamed it to "YAML Ain't Markup Language" (YAML is not just a markup language) to emphasize its data-oriented features .

yaml configuration file name: xxx.yml

Compared with others, show the advantages of yaml suitable for data-centric configuration files :

xml:

<server>
        <port>4040</port>
</server>

yaml:

tip: It can be seen that yaml is much simpler than xml, and there is no need to waste data on tags (emphasizing that yaml is data-centric), and the memory burden is greatly reduced 

yaml basic syntax

1. # indicates the comment content

2. Case sensitivity (attribute & value)

3. key: value (spaces should be used between kv ——> k:(space) v )

4. Indentation indicates the level, the indentation does not need the tab key, and the space is used to indicate that the same level only needs to be aligned on the left

5. The string can be without quotation marks, if you want to add: the content of ' ' will be escaped, and the content of " " will not be escaped

tips:

Single quotes vs double quotes:

        例:name :  ' Lily \n M '

               Output: Lily 

                          M

               name : " Lily \n M "

               Output: Lily \n M  

type of data:

Literals: A single, indivisible value. (eg: date, boolean, string, number, null)

k :  v

Object: A collection of key-value pairs. (eg: map, hash, set, object)

k :  v

#Inline writing vs normal writing

person:  { name: sana, age: 18 }

person: 

           name: sana

           age: 18 

Array: A group of values ​​arranged in order. Represents an element of an array with a -value. (eg: array, list, queue)

idol:

-wendy

-yuna

-sakura

#Inline writing

 idol: [wendy, yuna, sakura]

Acquisition of yaml configuration file value

1. Write a class under the bean package

Write basic data and its Getters and Setters methods [get..(), set..()], toString method (printing, in order to view the results, and compare and analyze the usage of different annotations)

2. Write the attribute value of the corresponding class in the yaml/properties configuration file (kv pair form)

tip: Pay attention to the hierarchical relationship

 

  3. Import the configuration file processor in the pom.xml file, and re-run the main program class to use/start this function

 4. Write annotations to make the function work

(1) @ConfigurationProperties is an annotation provided by springboot to read configuration files @ConfigurationProperties(prefix = "fill in the class name corresponding to the yaml/properties file"), which gets values ​​from the global configuration file by default

@Component : Instantiate ordinary pojo (object/entity class) into the spring container

Classes annotated by @Component will be included in the spring container for management

Detailed explanation : https://www.cnblogs.com/w-essay/p/11493023.htm

(2) @Value annotation (compared to @ConfigurationProperties)

  @value() reads out the properties of the configuration file, there are two ways @Value("${}") and @Value("#{}")

 

①  @Value("${property : default_value}"): When using the project built by the Springboot project, the configuration file application.properties has been loaded into the project, and the information in the configuration file can be obtained through this annotation in the project. The injection in ${} is the property corresponding to the external configuration file

②  @Value("#{}"): The content corresponding to the SpEL expression

tip: Spring Expression Language) is an expression language, something that can interact with runtime objects in a spring-based application; an expression that simplifies development

( https://www.csdn.net/tags/MtTaAgzsNjMyMjI1LWJsb2cO0O0O.html)

example:

A certain class (Person) under the bean package

@Value("#{ 11*5 }")

private Integer number; 

The application.properties file corresponding to this class (Person)

Person:

           number:18

Printout: Person{ number = 55 }

The obj in #{} represents the object. default_value, which is the default value when the previous value is empty

Detailed explanation : https://blog.csdn.net/qq_45597674/article/details/108820435

(3)@ConfigurationProperties  vs   @Value

(4) @PropertySource  loads the specified configuration file

 @ConfigurationProperties obtains values ​​from the global configuration file by default, but in order to avoid the problem of too large files caused by putting all configurations in the global configuration file, we can create other configuration files and use @PropertySource to load the specified configuration file

example:

@PropertySource(value  =  {"classpath: person.properties"})

(5) @ImportResource  imports the Spring configuration file to make the contents of the configuration file take effect

Example: (the annotation is placed on the main program class)

@ImportResource(locations = {"classpath: ....xml"}

 

Encoding problem of properties configuration file

The idea uses the UFT-8 code, while the properties use the ASCII code

If it is not set, it will print out garbled characters

Solution: Set the UFT-8 code at the bottom of the file encodings window of settings, remember to check it!

 

 

 

Guess you like

Origin blog.csdn.net/weixin_51583068/article/details/124786753