Springboot Configuration: Yaml Introduction

Source: Internet
Author: User

I. Introduction of YML

1. Spring boot uses a global configuration file (the name of the configuration file is fixed)

(1) application.properties

(2) Application.yml

2, the configuration file is placed in the Src/main/resources directory or the classpath/config

3, Yml is Yaml (yaml Ain ' t Markup lanaguage) language files, data-centric , more suitable for configuration files than Josn,xml. http://http:www.yaml.org reference Syntax specification.

Second, YAML syntax:

1. Basic syntax:

K: (Space) V: Indicates a pair of key-value pairs ( spaces must have )

Controls the hierarchical relationship with the indentation of a space, as long as the right-aligned column of data is a hierarchical

Properties and values are also case-sensitive.

Server:   8081   Path:/hello

2, the value of the wording

(1) Normal value (number, String, Boolean)

K:v: literally to write directly.

Strings are not added by default to single or double quotation marks;

  If you use double quotation marks : There are special characters in the string, special characters are not recognized and are not escaped.

For example:

Name: "Zhangsan \ Lisi" output: Zhangsan newline Lisi

  If single quotation marks are used , special characters are converted to normal character output.

Output: Zhangsan \ Lisi

(2) Map

K:v: Write the relationship of attributes and values in the next line, note the indentation

Object or the K:v way

friends:    lastname:zhangsan    

In-line notation:

(}

Array (List, Set):

  A value that represents an element in the array:

Pets:     - cat    - dog    -Pig

In-line notation:

Pets: [Cat,dog,pig]

Example:

Bean file

  

Package Com.song.bean;import Org.springframework.boot.context.properties.configurationproperties;import Org.springframework.stereotype.component;import java.util.date;import java.util.list;import Java.util.Map;/**j each attribute value in the configuration file is mapped to this array, *configurationproperties tells spring boot to bind all properties and configuration files in this class to the associated configuration * prefix = "person" : One by one mappings for all properties under that configuration file * Only this component is a component in the container to use the functionality provided by the container * Created by Administrator on 2018/6/12 0012. */@Component @configurationproperties (prefix=" Person") Public classPerson {PrivateString LastName; PrivateInteger age; PrivateBoolean Boos; PrivateDate Birth; PrivateMap<string,object>maps; PrivateList<object>lists; PrivateDog Dog; @Override PublicString toString () {return "person{"+"lastname= '"+ LastName +'\ ''+", age="+ Age +", boos="+ Boos +", Birth="+ Birth +", maps="+ Maps +", lists="+ Lists +", dog="+ Dog +'}'; }     PublicString Getlastname () {returnLastName; }     Public voidsetlastname (String lastName) { This. LastName =LastName; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }     PublicBoolean Getboos () {returnBoos; }     Public voidSetboos (Boolean boos) { This. Boos =Boos; }     PublicDate Getbirth () {returnbirth; }     Public voidSetbirth (Date birth) { This. Birth =birth; }     PublicMap<string, object>getmaps () {returnmaps; }     Public voidSetmaps (map<string, object>maps) {         This. maps =maps; }     PublicList<object>getlists () {returnlists; }     Public voidSetlists (list<object>lists) {         This. Lists =lists; }     PublicDog Getdog () {returnDog; }     Public voidSetdog (dog dog) { This. Dog =Dog; }}

Yaml file

Person:   Lastname:zhangsan   age:18   boss:true   BIRTH:2017/12/12   maps: {name: Floret, age:2}   lists :     -Lisi     -Zhaoliu   Dog:    name: Floret    age:2

Dependency configuration for Pom.xml

<!--Import the profile processor, the configuration file is bound with a hint-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

The final test prints the result as:

Person{lastname= ' Zhangsan ', age=18, Boos=null, Birth=tue Dec 00:00:00 gmt+08:00, maps={name= floret, age=2}, Lists=[li Si, Zhaoliu], dog=dog{name= ' Floret ', age=2}}

  

The corresponding properties file configuration

Person.lastname=zhangsanperson.age=28person.maps.k1=xiahuaperson.maps.k2=lisiperson.dog.name=xiaohua Person.dog.age=2person.lists=a,b,c

Springboot Configuration: Yaml Introduction

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.