SpringBoot学习(三)——yaml语法

yaml语法

springboot全局配置文件可以使用application.properties或者application.yaml或application.yml

yaml是配置文件 ,适合用来表达或编辑数据结构、各种配置文件

YAML 的配置文件后缀为 .yml/yaml,如:application.yml/application.yaml。

基本语法

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

数据类型

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

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

基本用法:

k: (空格) v 表示键值对

以缩进来表示层级关系 只要是左对齐的一列 都是同一个层级的

jdbc:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost:3306/SpringBoot
  username: root
  password: 123456
yaml实现属性值的注入

application.yaml

person:
  age: 18
  name: 张三
  birthday: 2003/9/18
  isstudent: true
  maps: {k1: v1,k2: v2}
  lists:
    - a
    - b
  dog:
    name: 小狗
    age: 1

Person.java

/**
 * @Component 将该bean 加入到spring容器
 * @ConfigurationProperties(prefix = "person") 告诉springboot 将本类中的所有的属性和配置文件中的相关的配置进行绑定
 * prefix = "person" :配置文件中的那个属性进行映射绑定
 *
 */

@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    int age;
    String name;
    Date birthday;
    boolean isstudent;
    Map maps;
    List lists;
    Dog dog;

    public Person(int age, String name, Date birthday, boolean isstudent, Map maps, List lists, Dog dog) {
        this.age = age;
        this.name = name;
        this.birthday = birthday;
        this.isstudent = isstudent;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }

    public Person() {
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public boolean isIsstudent() {
        return isstudent;
    }

    public void setIsstudent(boolean isstudent) {
        this.isstudent = isstudent;
    }

    public Map getMaps() {
        return maps;
    }

    public void setMaps(Map maps) {
        this.maps = maps;
    }

    public List getLists() {
        return lists;
    }

    public void setLists(List lists) {
        this.lists = lists;
    }

    public Dog getDog() {
        return dog;
    }

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

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

Dog.java

public class Dog {
    int age;
    String name;


    public Dog(int age, String name) {
        this.age = age;
        this.name = name;
    }

    public Dog() {
    }


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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


    @Override
    public String toString() {
        return "Dog{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

helloController.java

@RestController
public class helloController {

    @Autowired
    Person person;
    @RequestMapping("/hello")
    public String hello() {
        return person.toString();
    }
}

启动项目,访问:localhost:8080/hello

在这里插入图片描述

你可能感兴趣的:(SpringBoot,笔记,java,spring,boot,学习,java)