SpringBoot properties和yml的区别

一。先附一个yml文件的解析步骤

1.Maven依赖

  <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.10</version>
        </dependency>

2.yml文件

name: hha
age: 60
friend:
       - good
       - easy
       - bug
params:
      addr: ZZ
      code: EE
name: 洗洗

3.实体类

package com.my.last;

import java.util.List;
import java.util.Map;


public class Student {
    
    private String name;
    private int age;
    
    private List<String> friend ;
    
    private Map<String, Object> params;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * @return the friend
     */
    public List<String> getFriend() {
        return friend;
    }

    /**
     * @param friend the friend to set
     */
    public void setFriend(List<String> friend) {
        this.friend = friend;
    }

    /**
     * @return the params
     */
    public Map<String, Object> getParams() {
        return params;
    }

    /**
     * @param params the params to set
     */
    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", friend=" + friend + ", params=" + params + "]";
    }  

}

4.测试类

package com.my.last;

import org.yaml.snakeyaml.Yaml;

public class Test {
    public static void main(String[] args) {
        Yaml yaml = new Yaml();
        
        Student student = yaml.loadAs(Test.class.getResourceAsStream("/test.yml"), Student.class);
        System.out.println(student);
    }

}
org.ho.yaml.Yaml yaml2 = new org.ho.yaml.Yaml();
        Student student2 = null;
        try {
             student2 = org.ho.yaml.Yaml.loadType(Test.class.getResourceAsStream("/test.yml"), Student.class);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(student2);

 

<dependency>
            <groupId>org.jyaml</groupId>
            <artifactId>jyaml</artifactId>
            <version>1.3</version>
        </dependency>

解析方法:

猜你喜欢

转载自www.cnblogs.com/lukelook/p/11245832.html