SpringBoot 2 配置文件 2.2 yaml 格式 & 2.3 yaml 配置文件数据读取

SpringBoot

【黑马程序员2022新版SSM框架教程_Spring+SpringMVC+Maven高级+SpringBoot+MyBatisPlus企业实用开发技术】

2 配置文件

2.2 yaml 格式

properties 类型的配置文件在配置数据库四要素的时候使用过

YAML(YAML Ain’t Markup Language),一种数据序列化格式。【近些年主导】【且有一些优势】

最开始使用的xml:

<enterprise>
    <name>itcast</name>
    <age>16</age>
    <tel>4006184000</tel>
</enterprise>

而 properties 类型的配置文件如下

enterprise.name=itcast
enterprise.age=16
enterprise.tel=4006184000

yaml 类型的配置文件内容如下

enterprise:
	name: itcast
	age: 16
	tel: 4006184000

【优点】

  • 容易阅读

    yaml 类型的配置文件比 xml 类型的配置文件更容易阅读,结构更加清晰

  • 容易与脚本语言交互

  • 以数据为核心,重数据轻格式

    yaml 更注重数据,而 xml 更注重格式

【YAML 文件扩展名】

  • .yml【主流】
  • .yaml

使用更多还是yml,虽然都一样

2.2.1 语法规则
  • 大小写敏感

  • 属性层级关系使用多行描述,每行结尾使用冒号结束

  • 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)

  • 空格的个数并不重要,只要保证同层级的左侧对齐即可。

  • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)

  • # 表示注释

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

数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔,例如

enterprise:
  name: dingjiaxiong
  age: 22
  tel: 18888888888
  subject: 
    - Java
    - 前端
    - 人工智能

在这里插入图片描述

2.3 yaml 配置文件数据读取
2.3.1 环境准备

新创建一个名为 springboot_03_read_data 的 SpringBoot 工程模块

在这里插入图片描述

勾选web依赖

在这里插入图片描述

创建

在这里插入图片描述

创建控制器

package com.dingjiaxiong.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: BookController
 * date: 2022/9/21 16:27
 *
 * @author DingJiaxiong
 */

@RestController
@RequestMapping("/books")
public class BookController {
    
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id ===> " + id);
        return "Hello,SpringBoot!";
    }
    
}

创建一个名为 Enterprise 的实体类

package com.dingjiaxiong.domain;

import java.util.Arrays;

/**
 * ClassName: Enterprise
 * date: 2022/9/21 16:28
 *
 * @author DingJiaxiong
 */

public class Enterprise {
    
    private String name;
    private int age;
    private String tel;
    private String[] hobby;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Enterprise{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", hobby=" + Arrays.toString(hobby) +
                '}';
    }
}

在 resources 下创建一个名为 application.yml 的配置文件

lesson: SpringBoot

server:
  port: 80
  
enterprise:
  name: dingjiaxiong
  age: 22
  tel: 18888888888
  hobby:
    - Java
    - 前端
    - 深度学习
2.3.2 读取配置数据

【使用 @Value注解】

使用 @Value(“表达式”) 注解可以从配合文件中读取数据,

注解中用于读取属性名引用方式是: ${一级属性名.二级属性名……}

在 BookController 中使用 @Value 注解读取配合文件数据

package com.dingjiaxiong.controller;

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

/**
 * ClassName: BookController
 * date: 2022/9/21 16:27
 *
 * @author DingJiaxiong
 */

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

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

    @Value("${server.port}")
    private Integer port;

    @Value("${enterprise.hobby[0]}")
    private String hobby_00;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id ===> " + id);
        System.out.println(lesson);
        System.out.println(port);
        System.out.println(hobby_00);
        return "Hello,SpringBoot!";
    }

}

启动服务,访问接口

在这里插入图片描述

【Environment对象】

SpringBoot 还可以使用 @Autowired 注解注入 Environment 对象的方式读取数据。

这种方式 SpringBoot 会将配置文件中所有的数据封装到 Environment 对象中,如果需要使用哪个数据只需要通过调用Environment 对象的 getProperty(String name) 方法获取。

package com.dingjiaxiong.controller;

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

/**
 * ClassName: BookController
 * date: 2022/9/21 16:27
 *
 * @author DingJiaxiong
 */

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

    @Autowired
    private Environment environment;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id ===> " + id);

        System.out.println(environment.getProperty("lesson"));
        System.out.println(environment.getProperty("enterprise.name"));
        System.out.println(environment.getProperty("enterprise.hobby[2]"));

        return "Hello,SpringBoot!";
    }

}

重启服务器,访问接口

在这里插入图片描述

注意:这种方式,框架内含有大量数据,在开发中很少使用。

【自定义对象】

SpringBoot 还提供了将配置文件中的数据封装到我们自定义的实体类对象中的方式。

操作步骤:

  1. 将实体类 bean 的创建交给 Spring 管理。
    • 在类上添加 @Component 注解
  2. 使用 @ConfigurationProperties 注解表示加载配置文件
    • 在该注解中也可以使用 prefix 属性指定只加载指定前缀的数据
  3. 在 BookController 中进行注入

在这里插入图片描述

BookController:

package com.dingjiaxiong.controller;

import com.dingjiaxiong.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ClassName: BookController
 * date: 2022/9/21 16:27
 *
 * @author DingJiaxiong
 */

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

    @Autowired
    private Enterprise enterprise;

    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("id ===> " + id);

        System.out.println(enterprise.getName());
        System.out.println(enterprise.getAge());
        System.out.println(enterprise.getTel());
        for (int i = 0; i < enterprise.getHobby().length; i++) {
            System.out.println(enterprise.getHobby()[i]);
        }

        return "Hello,SpringBoot!";
    }

}

重启服务器,访问接口

在这里插入图片描述

注意:

使用这种方式时,在实体类上有警告提示

在这里插入图片描述

【解决办法】

在 pom.xml 中添加如下依赖即可

在这里插入图片描述

【亲测,无用】

还要在maven-compiler-plugin内的annotationProcessorPaths中添加相应path

<annotationProcessorPaths>
    <path>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>${spring-boot.version}</version>
    </path>
</annotationProcessorPaths>

问题解决

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ding Jiaxiong

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

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

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

打赏作者

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

抵扣说明:

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

余额充值