SpringBoot中yaml文件的使用

基本语法

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

数据类型

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

yaml在线转json:http://www.bejson.com/validators/yaml/
参考教程:https://www.runoob.com/w3cnote/yaml-intro.html

对象
Person:
  id: 1
  name: Lisa
  teacher:
    name: Mr.Lee
    age: 40

对应json:

{
  Person: {
    id: 1,
    name: 'Lisa',
    teacher: {
      name: 'Mr.Lee',
      age: 40
    }
  }
}

另一种写法:

Person: {name: Lisa,age: 18}

对应的json

{
  Person: {
    name: 'Lisa',
    age: 18
  }
}
数组

简单数组:

students:
  - stu1
  - stu2
  - stu3

teachers: [tea1,tea2,tea3]

对应json

{
  students: [
    'stu1',
    'stu2',
    'stu3'
  ],
  teachers: [
    'tea1',
    'tea2',
    'tea3'
  ]
}

对象数组:

Teacher:
  - id: 1
    name: stu1
    age: 18
  - id: 2
    name: stu2
    age: 19

对应json

{
  Teacher: [
    {
      id: 1,
      name: 'stu1',
      age: 18
    },
    {
      id: 2,
      name: 'stu2',
      age: 19
    }
  ]
}

通过application.yml文件给SpringBoot属性赋值

新建项目,导入依赖

其中lombok用于简化pojo实体类的编写,spring-boot-starter-test用于测试

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.3.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>
配置application.yml
person:
  id: 1
  name: Lisa
  happy: true
  weight: 93.2
  info: {school: qust,grade: 100}
  students:
    - id: 100
      name: LiJing
    - id: 200
      name: XiaoMing
编写实体类

注意,要将其注入的Spring的容器中

package com.qianyu.pojo;

import lombok.*;
import org.springframework.stereotype.*;

@Data
@Component
public class Student {
    private Integer id;
    private String name;
}
package com.qianyu.pojo;

import lombok.*;
import org.springframework.boot.context.properties.*;
import org.springframework.stereotype.*;

import java.util.*;

@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private Integer id;
    private String name;
    private boolean happy;
    private Double weight;
    private Map<String,Object> info;
    private List<Student> students;
}

@ConfigurationProperties注解可以将配置文件中的每一个属性的值,映射到指定组件中。即告诉SpringBoot将指定类中所有属性和配置文件中的属性进行绑定

编写主类、测试类

主类:

package com.qianyu;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

测试类:

package com.qianyu;


import com.qianyu.pojo.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.context.*;
import org.springframework.test.context.junit4.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
    @Autowired
    private Person person;

    @Test
    public void test() {
        System.out.println(person);
    }
}

运行结果:

Person(id=1, name=Lisa, happy=true, weight=93.2, info={school=qust, grade=100}, students=[Student(id=100, name=LiJing), Student(id=200, name=XiaoMing)])

说明属性注入成功!

拓展

*.properties文件配置属性

如果要使用*.properties类型文件注入的话,需要在类上添加@PropertySource(value="classpath:XXX.properties")指定配置文件路径,还要在类的属性上使用@Value()注解才能注入

不同位置application.yml文件的优先级

  1. file:./config/
  2. file:./
  3. classpath:/config/
  4. classpath:/

SpringBoot多环境

实现方式一:

  • 新建application-dev.ymlapplication-pro.yml两个文件,分别表示开发环境和测试环境
  • application.yml中指定环境
spring:
  profiles:
    active: dev

实现方式二:

将所有环境写在同一个application.yml中使用---分割

spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
server:
  port: 8081
---
spring:
  profiles: pro
server:
  port: 8082
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值