栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot学习(三)——yaml语法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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
@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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1003331.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号