4springboot configuration file and use yml

Profile
role:. Spring boot auto-configuration (convention, 8080) can be used to modify the configuration file for the default configuration

application.properties of modifications:

yml modifications:

 

Modify the default port 8080 to 8888, visit a success:

Default global profile:
  the application.properties: Key = value
  application.yml: Not myarkup YAML IS Language, not within a markup document written row (k: v, [Set / List / Array], {map, object type attribute} , and [] can be saved, not the province {}
  Note: 1 key:. blank value 

   2. The alignment is specified by a vertical hierarchy

   3.value default can not write quotes ; "" will be one of the escape character to escape, others will not

  server:
    port: 8882
    path: /a/b/c

  xml: is a markup document
  <server>

    <port>8882</port>
    <path>/a/b/c</path>
  </server>

 

 yml addition may be configured, you can also bind properties:

Example: class object to create an entity, and binding properties

package com.helloworld.entity;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component   // The entity class vessel was added ioc 
@ConfigurationProperties (prefix = " student ") // attribute corresponding binding declaration student here yml side of the student 
public  class Student {
     Private String name;
     Private  int Age;
     Private  Boolean Sex;
     Private a Date Birthday;
 //     {Province: Guangdong, city: Guangzhou, zone: by city} 
    Private the Map <String, Object> LOCATION;
     Private String [] hobbies;
     Private List <String> Skills;
     Private the Pet PET; // pets 
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public boolean isSex() {
        return sex;
    }
    public void setSex(boolean sex) {
        this.sex = sex;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public Map<String, Object> getLocation() {
        return location;
    }
    public void setLocation(Map<String, Object> location) {
        this.location = location;
    }
    public String[] getHobbies() {
        return hobbies;
    }
    public void setHobbies(String[] hobbies) {
        this.hobbies = hobbies;
    }
    public List<String> getSkills() {
        return skills;
    }
    public void setSkills(List<String> skills) {
        this.skills = skills;
    }
    public Pet getPet() {
        return pet;
    }
    public void setPet(Pet pet) {
        this.pet = pet;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", location="
                + location + ", hobbies=" + Arrays.toString(hobbies) + ", skills=" + skills + ", pet=" + pet + "]";
    }
    
}

Yml value is then injected in the configuration file:

student:
  name: zs
  age: 23
  sex: true
  birthday: 2019/02/12
  #location map is a collection of two input format
  # . 1 {K: space v}. Inline wording
  # 2 . Hierarchy represented by the vertical example: location:
                            #province: Guangdong
                            #city: Guangzhou
  location: {province: Guangdong, city: Guangzhou, zone: City} by the line set writing #map
  
  # Arrays and list \ set as input format - blank value or [football, basketball] brackets may be omitted in the line of writing
  hobbies: football, basketball array #
    # - Basketball
    # - Football
    
  skills: #list collection
    - Programming
     - Finance
  pet: # object type vertically aligned through k: v spaces or pet: {name: wc11, strain: hsq} inline wording
    name: wc
    strain: hsq

After the data binding in the test class inside a student assembly test objects:

 

 

 yml no properties actually printed out, the test is successful.

to sum up:

application.properties configuration: key = value

application.yml配置方式:

   1. key:空格value   简单属性配置

   2.通过垂直对齐 指定层次关系

   3.value默认可以不写引号; ""会将其中的转义符进行转义,其他不会

数据类型yml配置方式:

  1、简单数据   k:空格v

  2、list/set/数组

    2.1 行内写法:   hobbies: [足球,篮球]      中括号可省略

    2.2 垂直方式:hobbies:

            - 足球

            - 篮球

  3、对象/map

    3.1 location :{province: 广东,city: 广州,zone: 增城区}  行内写法  注意v前面有空格

    3.2 location :  

        province: 广东
        city: 广州            通过垂直对齐表示层次关系

Guess you like

Origin www.cnblogs.com/unlasting/p/12312854.html