Java學習筆記 -- yaml文件配置


yaml文件語法:

 

 

 

----------------------------實際操作---------------------------------

文件目錄:

 

創建Cat類:

package com.springbootpractise.pojo;

public class Cat {
    private String name;
    private Integer age;

    public Cat() {
    }

    public Cat(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

創建Person類:

package com.springbootpractise.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

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

@Component
@ConfigurationProperties(prefix="person")
//加載指定配置文件
//@PropertySource(value="classpath:qinjiang.properties")
public class Person {

    //SPEL表達式取出配置文件的值
   // @Value("${name}")
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Cat cat;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Cat cat) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.cat = cat;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", cat=" + cat +
                '}';
    }
}

yaml文件配置內容:

person:
  name: sunice
  age: 3
  happy: false
  birth: 2021/02/16
  maps: {k1: v1,k2: v2}
  lists: [1,2,3,4,5,cat]
  cat: {name: "",age: 3}

Test文件:

package com.springbootpractise;

import com.springbootpractise.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot02ConfigApplicationTests {

    @Autowired
    private Person person;  //配置Person類
    @Test
    void contextLoads() {
        System.out.println(person); //打印Person類
} }

運行結果:

Person{name='sunice', age=3, happy=false, birth=Tue Feb 16 00:00:00 CST 2021, maps={k1=v1, k2=v2}, lists=[1, 2, 3, 4, 5, cat], cat=Cat{name='', age=3}}

 

備注:

使用 @ConfigurationProperties(prefix="person") 后,IDEA會變紅,在pom.xml中添加依賴項即可解決問題
<!--解決 @ConfigurationProperties(prefix="person") 會變紅的問題-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--解決 @ConfigurationProperties(prefix="person") 會變紅的問題-->

 ----------------- yaml文件占位符----------------------------------------

 yaml文件中,也可以使用表達式進行賦值。${XXX}  是yaml文件中的占位符。XXX可以填寫表達式。

person:
  name: sunice${random.uuid}
  age: 3
  happy: false
  birth: 2021/02/16
  maps: {k1: v1,k2: v2}
  lists: [1,2,3,4,5,cat]
  hello: world
  cat:
    name: ${person.hello:hello}_貓

運行結果:

Person{name='sunice115c27cd-44e9-406c-87b3-ca98a4860974', age=3, happy=false, birth=Tue Feb 16 00:00:00 CST 2021, maps={k1=v1, k2=v2}, lists=[1, 2, 3, 4, 5, cat], cat=Cat{name='world_貓', age=null}}

 

---------------yaml文件松散綁定----------中划線和駝峰命名可以互相映射---------

 

 

 

 運行結果:

 

 

 

 

 參考資料:

https://www.bilibili.com/video/BV1PE411i7CV?t=146&p=9

https://www.bilibili.com/video/BV1PE411i7CV?p=10&spm_id_from=pageDriver

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM