yaml语法

 

配置文件:修改spring boot默认配置信息

yaml概念:

YAML 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。(官方)

#k= v
#普通的key-value
name: tj

#对象
person:
name: tj
age: 21

#行内写法
student: {name: tj,age: 21}

#数组
pets:
- cat
- dog
- pig
pets2: [cat,fog,pig]

普通的properties:只能储存键值对 yaml:可以储存对象,数组,键值对等

yaml文件实体类赋值

实体类Person

package cn.tj.springboot.entity;

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

import java.util.List;

@Component
@ConfigurationProperties(prefix = "person")
public class Person {
   private Integer id;
   private String name;
   private List<String> hobby;

   public Integer getId() {
       return id;
  }

   public void setId(Integer id) {
       this.id = id;
  }

   public String getName() {
       return name;
  }

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

   public List<String> getHobby() {
       return hobby;
  }

   public void setHobby(List<String> hobby) {
       this.hobby = hobby;
  }
}

yaml文件

person:
id: 1
name: tianjin${random.uuid}  #yaml文件可以加占位符${random.uuid}
hobby:
  - sleep
  - food
  - paly

通过@ConfigurationProperties(prefix = "person")获取实体类属性的值 ,只有组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能

默认根据application.yaml配置文件,如果要加载指定的yaml文件,通过@PropertySource(value = "class:path:(yaml文件)"),取值@Value(“${name}”

posted @ 2020-10-18 13:59  LastFairy  阅读(650)  评论(0)    收藏  举报