Spring Boot——第06讲yaml基础配置

一、yaml

        YAML(YAML  Ain't Markup Language),一种数据序列化格式。

优点:

  • 容易阅读
  • 容易与脚本语言交互
  • 以数据为核心,重数据轻格式

YAML文件扩展名:

  • .yml(主流)
  • .yaml

二、yaml语法规则

  • 大小写敏感
  • 属性层级关系使用多行描述,每行结尾使用冒号结束
  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  • #表示注解

核心规则:数据前面要加空格与冒号隔开 

country: china
province: beijing
city: beijing
area: haidian

port: 8080

party: true

birthday: 1949-10-01

user:
  name: jf
  age: 16

likes: [game,music,sleep]

users:
  - name: zhangsan
    age: 18
  - name: lisi
    age: 20
字面值表示方式
Boolean: TRUE#TRUE,true,True,FALSE,false,False均可
float: 3.14#6.8523015e+5     支持科学及算法
int: 123#0b1010_0111_0100_1010_1110          支持二进制、八进制、十六进制
null: ~#使用~表示null
string: hello world#字符串可以直接书写
string2: "hello world"#可以使用双引号包裹特殊字符
date:  2022-07-29#日期必须使用yyyy-MM-dd格式
datetime: 2022-07-29T15:02:31+08:00#时间和日期之间使用T连接,最后使用+代表时区

三、yaml数据读取

        使用@Value配合SpEL读取单个数据,属性名引用方式:${一级属性名.二级属性名...},遇到数组用中括号,如果数据存在多层级,依次书写层级名称即可,代码和效果图如下:

注意:user  默认有属性值  就是你系统 用户信息

@RestController
@RequestMapping("/books")
public class BookController {

    //读取yaml数据中的单一数据
    @Value("${country}")
    private String country1;

    @Value("${user.name}")
    private String name1;

}

四、yaml文件中的变量引用

1. 使用${属性名}引用数据

#使用${属性名}引用数据
tempDir: "${baseDir}\temp"

2. 属性中如果出现转义字符,需要使用双引号包裹

#使用引号包裹的字符串,其中的转义字符可以生效
tempDir2: "${baseDir}\temp\t1\t2\t3"

        注意:在yaml中是支持转义字符的,例如上图的例子只要加上引号效果就会不一样

五、读取yaml全部属性数据

封装全部数据到 Environment 对象

使用 @Autowired 自动装配数据到 Environment 对象中

Controller代码: 

package com.jf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;

//Rest模式
@RestController
@RequestMapping("/books")
public class BookController {

    //读取yaml数据中的单一数据
    @Value("${country}")
    private String country1;

    @Value("${user.name}")
    private String name1;

    @Value("${tempDir}")
    private String tempDir;

    //使用自动装配将所有的数据封装到一个对象Environment中
    @Autowired
    private Environment env;

   @GetMapping
    public String getById(){
        System.out.println("springboot is running...");
        System.out.println("country1===>" + country1);
       System.out.println("name1===>" + name1);
       System.out.println("tempDir===>" + tempDir);
       System.out.println("--------------------------");
       System.out.println(env.getProperty("user.name"));
       System.out.println(env.getProperty("country"));
        return "springboot is running...";
    }
}

 yml代码:

country: china
province: beijing
city: beijing
area: haidian

port: 8080

party: true

birthday: 1949-10-01

user:
  name: jf
  age: 16

likes: [game,music,sleep]

users:
  - name: zhangsan
    age: 18
  - name: lisi
    age: 20

server:
  port: 81

baseDir: c:\windows

#使用${属性名}引用数据
tempDir: "${baseDir}\temp"
#使用引号包裹的字符串,其中的转义字符可以生效
tempDir2: "${baseDir}\temp\t1\t2\t3"

六、读取yaml引用类型属性数据

1. 自定义对象封装指定数据

datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/springboot_db
  username: root
  password: 123123

提供一个用来封装的模型类

package com.jf;

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

//1.定义数据模型封装yaml文件中对应的数据
//2.定义为spring管控的bean
@Component
//3.指定加载的数据
@ConfigurationProperties(prefix = "datasource")
public class MyDataSource {
    private String driver;
    private String url;
    private String username;
    private String password;

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "MyDataSource{" +
                "driver='" + driver + '\'' +
                ", url='" + url + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

@component (把普通pojo实例化到spring容器中,相当于配置文件中的 <bean id="" class=""/>)

2. 启动

        使用 @ConfigurationProperties 注解绑定配置信息到封装类中,封装类需要定义为Spring管理的Bean,否则无法进行属性注入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值