yaml处处处处处处处处处处体验。

本文详细介绍了YAML配置语言的基本语法和数据类型,包括对象、数组和纯量的表示方式。并通过实例展示了如何在SpringBoot项目中使用YAML进行配置,实现配置文件与实体类的自动绑定,以及占位符的高级应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入图片描述
搞一搞yaml,搞起。
YAML 是 “YAML Ain’t a Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。(那你到底是不是呢?谮纠结。)
YAML 的语法和其他高级语言类似,并且可以简单表达清单、散列表,标量等数据形态。它使用空白符号缩进和大量依赖外观的特色,特别适合用来表达或编辑数据结构、各种配置文件、倾印调试内容、文件大纲(例如:许多电子邮件标题格式和YAML非常接近。

基本语法

  • 大小写敏感
  • 使用缩进表示层级关系 缩进不允许使用tab,只允许空格
  • 缩进的空格数不重要,只要相同层级的元素左对齐即可
  • '#'表示注释

数据类型

YAML 支持以下几种数据类型:

  • 对象:键值对的集合,又称为映射(mapping)/ 哈希(hashes) / 字典(dictionary)
  • 数组:一组按次序排列的值,又称为序列(sequence) / 列表(list)
  • 纯量(scalars):单个的、不可再分的值
    -在这里插入图片描述

Yaml操作

对象
对象键值对使用冒号结构表示 key: value,冒号后面要加一个空格。
也可以使用 key:{key1: value1, key2: value2, …}。
还可以使用缩进表示层级关系;

User:
  name: lidadaibiao
  age: 2
  sex: 男
  #行内写法 同上
Users: {name: lidadaibiao,age: 2,sex:}

数组
以 - 开头的行表示构成一个数组

Students:
  - dadaibiao1
  - dadaibiao2
  - dadaibiao3
#同上
Student: [dadaibiao1,dadaibiao2,dadaibiao3]

注意:
对空格要求特别高
能够配置到我们的配置类中

Ymal可以直接给实体类赋值吗?试一下吗。

在这里插入图片描述

测试如下:
首先先编写两个实体类:

package com.example.springboot01.pojo;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author xuedong Li
 * @version V1.0
 * @Package com.example.springboot01.pojo
 * @date 2020/8/4 19:00
 */
@Component //将实体类以组件的形式添加到spring组件中
@Data
public class Student {
    //spring中 给属性赋值的注解
    @Value("dadaibiao")
    private String name;
    @Value("11")
    private Integer age;
}

package com.example.springboot01.pojo;

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

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

/**
 * @author xuedong Li
 * @version V1.0
 * @Package com.example.springboot01.pojo
 * @date 2020/8/4 19:10
 */
@Component //成为spring组件为了被扫描到 也就是注册bean
@Data
//ConfigurationProperties
// 作用:
// 将配置文件中每一个属性的值,映射到这个组件中,告诉springboot将本类中的所有属性和配置文件中相关配置进行绑
// 定:prefix = "teacher" 这句话就是和yaml中名称为teacher的配置,进行绑定,属性一一对应。
//只有这个组件是spring容器中的组件,才可以使用容器提供的这个注解
@ConfigurationProperties(prefix = "teacher")//通过该配置和前缀可以和yaml中的配置绑定起来
public class Teacher {
    private String name;
    private Integer age;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;
    private Student student;
}

测试类简单测试一下,是否注入成功

package com.example.springboot01;

import com.example.springboot01.pojo.Student;
import com.example.springboot01.pojo.Teacher;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Springboot01ApplicationTests {
    @Autowired
    //spring中的自动注入组件
    private Student student;
    @Test
    void contextLoads() {
        System.out.println(student);
       
    }

}

在这里插入图片描述
注入成功。
开始编写yaml配置文件

teacher:
  name: dadaibiao
  age: 22
  birth: 2020/08/04
  maps: {k1: daibiao1,k2: daibiao2}
  lists:
    - money
    - bgril
    - bus
  student:
    name: dadaibiao
    age: 1

在这里插入图片描述
在这里插入图片描述同student一样测试一下
在这里插入图片描述
成功、
延伸:搞一些高级的东西,占位符的使用

teacher:
  name: dadaibiao${random.uuid}
  age: 22
  birth: 2020/08/04
  ok: ko
  maps: {k1: daibiao1,k2: daibiao2}
  lists:
    - money
    - bgril
    - bus
  student:
    name: ${teacher.ok:ok}+++dadaibiao
    age: 1

在这里插入图片描述
测试正常,完美实行。

在这里插入图片描述

客官,下次再见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值