[SpringBoot] SpringBoot YAML configuration files and Introduction

SpringBoot profile

  SpringBoot use a global configuration file, the configuration file name is fixed; 

  • application.properties
  • application.yml

  Profile role: Modify the default value SpringBoot auto-configuration; SpringBoot at the bottom is automatically configured;

About YAML

  AML (YAML Is not markup language)

  YAML A Markup Language: is a markup language

  YAML is not Markup Language: is not a markup language;

  Markup Language: the previous configuration file; most of them are using xxxx.xml file; YAML: data-centric than json, xml and so is more suitable for the configuration file;

  YAML: Configuration example

1 server:
2   port: 8081

  properties file:

1 server.port= 8081

  YAML Syntax

  1, the basic grammar

    k :( space) v

    Represent a pair of key-value pairs (there must be a space); a space of indentation control hierarchy; as long as one is left-aligned data, are the same level;

    And attribute values ​​are case sensitive;

1 server:
2   port: 8081
3   path: /hello

  2, the value written

    Literal: Common values ​​(numbers, strings, Boolean)

    k: v == literal directly to write;

    No default string in single or double quotes; 

    "": Double quotes; there will not escape the string of special characters; special characters as themselves wish to express the meaning of name: "zhangsan \ n lisi": output; zhangsan wrap lisi

    '': Single quote; will escape special characters, special characters finally just an ordinary string data name: 'zhangsan \ n lisi': output; zhangsan \ n lisi

    Object, Map (attributes and values) (key-value pairs):

    k: v == relationship to write the next line object attributes and values; note indentation

    Object or k: v way

1 friends:
2   lastName: zhangsan
3   age: 20

    Inline wording:

1 friends: {lastName:zhangsan, age:18}
    Array (List, Set):

    Represents an element in the array values ​​- with

1 pets: 
2   ‐cat
3   ‐dog
4   ‐pig

    Inline wording

1 pets: [cat,dog,pig]

  3, the injection profile values

    Profiles

 1 person:
 2   lastName: hello
 3   age: 18
 4   boss: false
 5   birth: 2017/12/12
 6   maps: {k1: v1,k2: 12}
 7   lists:
 8     ‐ lisi
 9     ‐ zhaoliu
10   dog:
11     name: 小狗 
12     age: 12

    javaBean

1  * / 
2  * profile value for each attribute in the configuration mapped to the component
 . 3  * @ConfigurationProperties all the attributes and the profile of this class bind the relevant configuration;
 . 4  * prefix = " person ": all of which profile following properties
 5  * this component is the only component of the container, this container can be used provided ah @ConfigurationProperties function
 . 6  *
 . 7   * / 
. 8  // @PropertySource (value = {" CLASSPATH: Person .properties "}) 
. 9  @Component
 10 @ConfigurationProperties (prefix =" Person " )
 . 11  public  class the Person {
 12 is  
13 is      Private String lastName;
 14      Private Integer Age;
 15     private Boolean boss;
16     private Date birth;
17 
18     private Map<String, Object> maps;
19     private List<Object> lists;
20 
21     private Dog dog;

    Note: The import configuration file processor that pom file add the following dependence later in the preparation of the configuration file have prompted the

1  <! - import configuration file processor, binding profile will be prompted -> 
2  < dependency > 
. 3      < the groupId > org.springframework.boot </ the groupId > 
. 4      < the artifactId > Spring-Configuration-Processor Boot- </ the artifactId > 
. 5      < optional > to true </ optional > 
. 6  </ dependency >

    a, *. properties file garbage problem, idea default utf-8 may be garbled, adjust settings

      

    b, @ Value acquisition value and acquisition value comparison @ConfigurationProperties

      

 

@ConfigurationProperties

@Value

Features

Batch injection configuration file attributes

A designated

Loosely bound (loose syntax)

stand by

not support

Game

not support

stand by

JSR303 data check

stand by

not support

Complex type package

stand by

not support

    

      Profile yml or properties they can get to value;

      If you say that just need to get some business logic at a particular value in the configuration file, use @Value;

      If we say that a specially prepared javaBean and configuration files to be mapped, we just use @ConfigurationProperties;

    c, injection profile data checksum value

      

Guess you like

Origin www.cnblogs.com/h--d/p/11986670.html