yaml基礎語法、給屬性賦值、和properties的區別


yaml基礎語法

說明:語法要求嚴格!

1、空格不能省略

2、以縮進來控制層級關系,只要是左邊對齊的一列數據都是同一個層級的。

3、屬性和值的大小寫都是十分敏感的。

yaml給屬性賦值

@ConfigurationProperties 作用:

將配置文件中配置的每一個屬性的值,映射到這個組件中。告訴spingboot將本類中的所有屬性和配置文件中相關的配置進行綁定。

參數prefix = "person" :將配置文件中的person下面的所有屬性一一對應。

(只有這個組件是容器中的組件才能使用容器提供的@ConfigurationProperties 功能)

person.java

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

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

@Component	//注冊bean
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;

    public Person() {
    }

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

    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;
    }

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

application.yaml

person:
  name: wen
  age: 18
  happy: true
  birth: 2000/01/01
  maps: {k1: v1,k2: v2}
  lists:
    - code
    - sing
    - draw

Boot01ApplicationTests.java

@SpringBootTest
class Boot01ApplicationTests {
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
        System.out.println(person);
    }

}

運行結果:

另外:配置文件除了yaml還有properties,properties配置文件在寫中文的時候會有亂碼,我們需要去IDEA中設置編碼格式為UTF-8

yaml和properties的區別

總結:

  • 配置yml和配置properties都可以獲取到值,強烈推薦yml
  • 如果我們在某個業務中,只需要獲取配置文件中的某個值,可以使用一下@value
  • 如果我們專門編寫了一個JavaBean來和配置文件進行映射,就直接使用@configurationProperties

松散綁定

(這里的firstName和first-name是一樣的,-后面的字母默認是大寫的)

結果:

JSR303校驗

需要添加依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

yaml里的格式不是郵箱格式,所以會報錯:


免責聲明!

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



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