SpringBoot yml配置文件如何读取

本篇内容主要讲解“SpringBoot yml配置文件如何读取”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot yml配置文件如何读取”吧!

yaml介绍

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

优点:

  • 容易阅读

  • 容易与脚本语言交互

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

YANL文件扩展名

  • .yml(主流)

  • .yaml

几种数据格式比较

yaml语法规则

  • 大小写敏感

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

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

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

  • #表示注释

示例:

user:
  name: zhangsan
  age: 12
users:
  -
    name: lisi
    age: 13
  -
    name: wangwu
    age: 18
likes: [game,play,having]
users1: [{name:zhangsan,age:12},{name:lisi,age:12}]

字面值表示方式

数组表示方式:在属性名书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据键空格分隔

yaml数据读取

使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名}

controller下

package com.springboot01_02.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/SpringBoot")
public class TestController {
@Value("${user.name1}")
    private String  username1;
@Value("${users[0].age}")
private String userage2;
@Value("${person.hobby[0]}")
private String personHobbyEat;
    @Value("${person.hobby[1]}")
    private String personHobbyPlay;
    @Value("${users1[0].age}")
    private String usersAge;
    @RequestMapping("/test")
    public String Test(){
        System.out.println("username->"+username1);
        System.out.println("userage->"+userage2);
        System.out.println("personHobbyEat->"+personHobbyEat);
        System.out.println("personHobbyPlay->"+personHobbyPlay);
        System.out.println("usersAge->"+usersAge);
        return "springboot is good";
    }
}

yml配置文件

user:
  name1: KC
  age: 12
users:
  -
    name: lisi
    age: 13
  -
    name: wangwu
    age: 18
person:
  name: ZH
  age: 19
  tel: 152161
  hobby:
    - eat
    - play
    - run
users1: [{name: zhangsan,age: 12},{name: lisi,age: 12}]
likes: [game,play,having]

运行结果:

yaml数据读取

在配置文件中可以使用属性名引用方式引用属性

在配置文件中

package com.springboot01_02.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/SpringBoot")
public class TestController {
    @Value("${nowDir}")
    private String nowDir1;
    @Value("${tewDir}")
    private String tewDir1;
    @RequestMapping("/test")
    public String Test(){
        System.out.println("nowDir->"+nowDir1);
        System.out.println("towDir->"+tewDir1);
        return "springboot is good";
    }
}

运行结果:

可以发现,要想让转义字符生效,就得加上双引号不然还是以字符串的形式打印出

Environment读取yaml全部属性数据

package com.springboot01_02.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.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/SpringBoot")public class TestController {//使用自动装配将所有数据封装到一个Environment中    @Autowired    private Environment evn;    @RequestMapping("/test")    public String Test(){        System.out.println("----------------");        System.out.println(evn.getProperty("nowDir"));        System.out.println(evn.getProperty("users1[0].age"));        return "springboot is good";    }}

运行结果:

小结:

使用Environment对象封装全部配置信息

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

自定义对象封装指定数据

application.yaml配置文件中的信息

#创建类,用于封装下面的数据#有spring带我们去加载数据到对象中,且告诉spring加载这组信息#使用时从spring中直接获取信息使用datasource:  driver: com.mysql.jdbc.Driver  url: jdbc:mysql://localhost/love  username: kongchao  password: zenghui

自定义一个类

package com.springboot01_02.datesource;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;//1、定义数据类型模型封装yaml文件中对应的数据//2、定义为spring管控的bean@Component//3、指定加载的数据@ConfigurationProperties("datasource")//4、设置getSet方法等@Datapublic class MyDateSource {    private  String driver;    private  String url;    private  String username;    private  String password;}

使用了@Date,在pom.xml中导入lombok

        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <version>1.16.10</version>        </dependency>

测试类下

package com.springboot01_02.controller;import com.springboot01_02.datesource.MyDateSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/SpringBoot")public class TestController {@Autowired    private MyDateSource dateSource;    @RequestMapping("/test")    public String Test(){        System.out.println(dateSource);        return "springboot is good";    }}

运行访问localhost/SpringBoot/test即可得到:

小结:

使用@ConfigurationProperties注解绑定配置信息到封装类中

封装类需要定义为Spring管理的bean(使用注解@Component),否则无法进行属性注入

到此,相信大家对“SpringBoot yml配置文件如何读取”有了更深的了解,不妨来实际操作一番吧!这里是本站网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

相关推荐:

Springboot如何查询MySQL DATE字段?

springboot日期查询mysql date字段时的问题 在使用springboot查询mysql date字段时,可能会遇到日期类型不匹配的问题。这是因为springboot接收时间时默认为timestamp类型,而mysql date字段是日期类型。 解决方案 解决方法是将前端传来的日期字符串直接转换成date类型,然后使用mybatis-plus进行查询。代码示例如下:// 后端参数接收...

YAML vs YML:差异是什么,您应该使用哪个?

YAML和YML:配置文件格式的细微差别 YAML(YAML Ain't Markup Language)和YML经常让开发者困惑,尤其是在使用配置文件时。两者实际上指的是同一种数据序列化格式,区别仅仅在于文件扩展名。本文将阐明YAML和YML之间的细微差别、共同点以及各自的使用场景。 YAML是什么? YAML是一种人类可读的数据序列化语言,以其简洁明了的语法而闻名。它广泛应用于Docker、K...

springboot怎么集成shiro框架

要在Spring Boot项目中集成Apache Shiro框架,可以按照以下步骤进行操作: 添加依赖:在pom.xml文件中添加Shiro和Spring Boot Shiro相关的依赖。例如: <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot...

SpringBoot CommandLine如何配置

SpringBoot CommandLine是一个用于构建命令行应用的工具。要配置SpringBoot CommandLine,可以按照以下步骤进行操作: 添加依赖:在项目的pom.xml文件中添加SpringBoot CommandLine的依赖。 <dependency> <groupId>org.springframework.boot</groupId>...

SpringBoot CommandLine的优势何在

Spring Boot CommandLine 的优势主要有以下几点: 简化开发流程:Spring Boot CommandLine 可以帮助开发者快速构建命令行应用程序,简化了配置和开发流程。开发者只需要关注业务逻辑的实现,而不需要花费大量的时间去配置项目。 集成了常用功能:Spring Boot CommandLine 提供了许多常用的功能和组件,例如参数解析、命令行选项、日志记录等,开发者可...

使用SpringBoot CommandLine需要注意什么

在使用Spring Boot CommandLine时,需要注意以下几点: 引入必要的依赖:在pom.xml文件中添加Spring Boot CommandLine的依赖,如spring-boot-starter,spring-boot-starter-web等。 创建主应用程序类:创建一个包含main方法的主应用程序类,并使用@SpringBootApplication注解标记它。 实现命令行应...

SpringBoot CommandLine可以做哪些事情

Spring Boot CommandLine 可以用于以下几个方面: 运行Spring Boot应用程序:使用CommandLine可以直接在命令行中启动Spring Boot应用程序,而不需要使用IDE或者构建工具。 执行自定义命令:通过CommandLine可以执行自定义命令,例如初始化数据库、调用外部API、生成报告等。 与操作系统交互:可以通过CommandLine与操作系统交互,执行系...

SpringBoot CommandLine怎样解析参数

SpringBoot应用程序可以通过CommandLineRunner接口来解析命令行参数。以下是一个简单的示例: 首先,创建一个CommandLineRunner接口的实现类,并实现run方法: import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @...