SpringBoot - yame语法规则 && 读取yaml数据案例测试(补充)

yaml文件中变量引用

遇到问题

相同文件路径名,数据较多时,改的时候不能一起改,所以我们直接引用这个路径

请添加图片描述

改为

请添加图片描述

尝试读取

package com.taotao.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

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

    @GetMapping
    public String getById(){
        System.out.println("spring boot is running");
        System.out.println("port====>" + temp);
        return "spring boot is running";
    }
}

测试成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pOD28rAu-1651377282286)(springboot.assets/image-20220501102649277.png)]

yaml读取支持转义字符

要使用双引号引起来

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VWq3fiHx-1651377282287)(springboot.assets/image-20220501103130928.png)]

测试成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-y01fLXlu-1651377282287)(springboot.assets/image-20220501103140896.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pZX6M30V-1651377282288)(springboot.assets/image-20220501103224164.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5eDot3e6-1651377282289)(springboot.assets/image-20220501103231286.png)]

yaml读取全部属性数据

使用Environment对象封装信息

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

package com.taotao.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

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

    @GetMapping
    public String getById(){
        System.out.println("spring boot is running");
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user.name"));
        return "spring boot is running";
    }
}

测试成功

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H3CD8OL1-1651377282290)(springboot.assets/image-20220501104409463.png)]

注意

Environment导入的是SpringBoot包下的接口

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jWldKLZB-1651377282290)(springboot.assets/image-20220501104153307.png)]

读取yaml引用类型属性数据

datasource:
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/text
  username: root
  password: 12345

编写实体类

package com.taotao;

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

/**
 * create by 刘鸿涛
 * 2022/5/1 11:36
 */
@SuppressWarnings({"all"})
//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 + '\'' +
                '}';
    }
}

读取

package com.taotao.controller;


import com.taotao.MyDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by 刘鸿涛
 * 2022/4/28 16:39
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {

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

    private MyDataSource myDataSource;

    @GetMapping
    public String getById(){
        System.out.println("spring boot is running");
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("user.name"));
        System.out.println("------------------------");
        System.out.println(myDataSource);
        return "spring boot is running";
    }
}

读取测试(null)

没有对对象进行自动装配

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sWx3ZfkR-1651377282291)(springboot.assets/image-20220501114841883.png)]

成功测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SkOl2hvX-1651377282292)(springboot.assets/image-20220501114933234.png)]

servlet: context -path

访问前缀

server:
  port: 80
  servlet:
    context-path: /taotao

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oHFRWmLh-1651377282293)(springboot.assets/image-20220501115140519.png)]

小结

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6brpoA7a-1651377282294)(springboot.assets/image-20220501115244121.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dHPghOwP-1651377282295)(springboot.assets/image-20220501115340258.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NQ1df4q8-1651377282297)(springboot.assets/image-20220501115357740.png)]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鬼鬼骑士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值