Configuration file details

Source: Internet
Author: User

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.

  • name: "sihai\n lisi"

     Output: Sihai line feed Lisi ": single quotes: special characters are escaped. special characters are only common string data.

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.

  • user:    name: sihai    age: 20

    In-line writing:

  • user: {name: sihai, age: 18}

     

  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

  • User: Name: Sihai age: 20 lists:-Lisi-zhangsan pig: Name: Piglet age: 10

     

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.