SpringBoot学习(三)——yaml语法

78 篇文章 0 订阅
10 篇文章 0 订阅
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

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值