当前位置:   article > 正文

yaml语法讲解

yaml语法

YAML 是一个可读性高,用来表达数据序列化的格式。YAML也参考了其他的各种语言,包括:C语言Python等,并从XML的数据格式中获得灵感。从而创造出的语言格式

Y A M L 语 法 \color{#0000FF}{YAML语法} YAML

# K是属性名 v是属性值
k: v
  • 1
  • 2

Y A M L 表 示 对 象 \color{#FF7D00}{YAML表示对象} YAML

people:
  name:kisa
  age:3
# 行内写法
people: {name:kisa,age:3}
  • 1
  • 2
  • 3
  • 4
  • 5

Y A M L 表 示 数 组 \color{#FF7D00}{YAML表示数组} YAML

pets: 
  -cat,
  -dog
 # 行内写法
 pets: [cat,dog]
  • 1
  • 2
  • 3
  • 4
  • 5

Y A M L 给 属 性 赋 值 \color{#0000FF}{YAML给属性赋值} YAML

代 码 实 例 \color{#0000FF}{代码实例}

创建Dog实体类

package com.j.po;

import org.springframework.stereotype.Component;

/**
 * Created by jdx on 2022/3/17 上午10:47
 */
@Component
public class Dog {
    private String name;
    private Integer age;

    public Dog() {
    }

    public Dog(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 "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

创建People实体类

package com.j.po;

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

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

/**
 * Created by jdx on 2022/3/17 上午10:47
 */
//标注一个类为Spring容器的Bean,(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
@Component
//引用yaml文件并set值
@ConfigurationProperties(prefix = "people")
public class People {
    private String name;
    private Integer age;
    private Boolean happy;
    private Date birth;
    private List<Object> list;
    private Dog dog;
    private Map<String, Object> map;
    /*无参构造*/
    public People() {
    }

    /*有参构造*/
    public People(String name, Integer age, Boolean happy, Date birth, List<Object> list, Dog dog, Map<String, Object> map) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.list = list;
        this.dog = dog;
        this.map = map;
    }
    /*get set方法*/

    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 List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public Map<String, Object> getMap() {
        return map;
    }

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

    /*toString方法*/
    @Override
    public String toString() {
        return "people{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", list=" + list +
                ", dog=" + dog +
                ", map=" + map +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

解 决 S p r i n g B o o t C o n f i g u r a t i o n A n n o t a t i o n P r o c e s s o r n o t c o n f i g u r e d \color{#FF00FF}{解决Spring Boot Configuration Annotation Processor not configured} SpringBootConfigurationAnnotationProcessornotconfigured

<!--解决不影响运行的报错依赖:Spring Boot Configuration Annotation Processor not configured-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

创建set值yaml文件

people:
  name: MJl
  age: 18
  happy: true
  birth: 1999/01/11
  list:
    - code
    - music
    - girl
  dog:
    name: 来福
    age: 3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

创建ApplicationTests测试类

package com.j;

import com.j.po.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class ApplicationTests {

    @Resource
    private People people;
    @Test
    void contextLoads() {
        System.out.println(people);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

运 行 结 果 \color{#FF00FF}{运行结果}

people{name='MJl', age=18, happy=true, birth=Mon Jan 11 00:00:00 CST 1999, list=[code, music, girl], dog=Dog{name='来福', age=3}, map=null}
  • 1

以上代码实现逻辑来看yaml是可以作为set注入的一种方式

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号