yml

1 简介

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

非常适合用来做以数据为中心的配置文件


2 基本语法

key: value;kv之间有空格

大小写敏感

使用缩进表示层级关系

缩进不允许使用tab,只允许空格

缩进的空格数不重要,只要相同层级的元素左对齐即可

'#'表示注释

字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义


3 数据类型

  1. 字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
  1. 对象:键值对的集合。map、hash、set、object
行内写法:  k: {k1:v1,k2:v2,k3:v3}
#或
k: 
	k1: v1
  k2: v2
  k3: v3

4 实例

  1. 创建两个bean
@Data
public class Person {

    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}
@Data
public class Pet {
    private String name;
    private Double weight;
}
  1. yml配置文件(application.yml)创建bean的对象
person:
  userName: zhangsan
  boss: true
  birth: 2019/12/9
  age: 18
#  interests: [篮球,足球]
  interests:
    - 篮球
    - 足球
    - 18
  animal: [阿猫,阿狗]
#  score:
#    english: 80
#    math: 90
  score: {english: 80,math: 90}
  salarys:
    - 9999.98
    - 9999.99
  pet:
    name: 阿狗
    weight: 99.99
  allPets:
    sick:
      - {name: 阿狗,weight: 99.99}
      - name: 阿猫
        weigth: 88.88
      - name: 阿虫
        weight: 77.77
    health:
      - { name: 阿花,weight: 199.99 }
      - { name: 阿明,weight: 199.99 }
  1. controller文件
@RestController
public class helloController {
    @Autowired
    Person person;

    @RequestMapping("/person")
    public Person person() {
        return person;
    }
}
  1. 浏览器中查看显示结果

  2. 注意:application.properties和application.yml都是配置文件,但是application.properties的优先级要高于后者。

5 配置提示

给application.yml配置文件编写代码时添加提示,

在pom.xml文件中:

添加配置提示:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

打包时删除相应jar包:

<configuration>
    <excludes>
        <exclude>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </exclude>
    </excludes>
</configuration>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值