小工具知识网

当前位置: 首页 > 行业知识 > SpringBoot中读取yml配置文件中的数据的方式

行业知识

SpringBoot中读取yml配置文件中的数据的方式

2026-11-07 16:29:52 来源:互联网转载

我们可以通过以下三种方式获取到配置文件里面的数据

1.定义变量使用@Value注解来读取

我们可以直接在程序中定义变量,然后通过Spring中提供的注解@Value来给变量赋值,然后就可以直接使用,代码示范如下:

package com.hua.springbootdemo01introduce.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;@RestController@RequestMapping("/books")public class BookController {    /**     * 读取yml配置中的数据有三种方式     * 1.直接定义变量然后通过@Value注解赋值读取     * 2.通过Environment对象读取     * 3.通过封装实体类来读取     */    @Value("${server.port}")    private String portNum;    @Value("${entries.subject[2]}")    private String subject_02;    @GetMapping("/{id}")    public String queryById(@PathVariable Integer id) {        System.out.println("获取到的端口号为:"+portNum);        System.out.println("获取到的subject信息为:"+subject_02);        return "hello,SpringBoot!";    }}

结果如下:

2.使用Environment对象读取

我们还可以通过一个对象来获取到这些信息,这个对象就是Environment对象,这个对象代表了当前运行环境的一些属性和配置信息。它提供了访问和管理这些环境属性的方法。我们可以在代码中注入该属性,然后调用对象的get方法获取。代码示范如下:

package com.hua.springbootdemo01introduce.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.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {    /**     * 读取yml配置中的数据有三种方式     * 1.直接定义变量然后通过@Value注解赋值读取     * 2.通过Environment对象读取     * 3.通过封装实体类来读取     */    //自动装配,将所有的环境中的值装配到environment对象里面    @Autowired    private Environment environment;    @GetMapping("/{id}")    public String queryById(@PathVariable Integer id) {        System.out.println(environment.getProperty("server.port"));        System.out.println(environment.getProperty("entries.subject[2]"));        return "hello,SpringBoot!";    }}

结果如下:

3.封装实体类读取

我们还可以通过将配置中的数据封装成一个实体类,实体类会自动映射配置中的数据,然后通过这个实体类就可以访问配置中的一些数据。其中会用到SpringBoot中提供的配置注解@ConfigurationProperties,从这个方法也可以看出我们配置文件一定要遵守规范书写,否则就会读取不准确,示范代码如下:

实体类的封装:

package com.hua.springbootdemo01introduce.pojo;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * 这种方式可以将这个实体类映射到yml配置中的配置类 */@Data@Component//想让spring可以操控这个类,就必须将他注入进spring容器中@ConfigurationProperties(prefix = "entries")//指定配置数据中的key值public class Entries {    private String name;    private Integer age;    private String[] subject;}

使用实体类: 

package com.hua.springbootdemo01introduce.controller;import com.hua.springbootdemo01introduce.pojo.Entries;import org.springframework.beans.factory.annotation.Autowired;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;@RestController@RequestMapping("/books")public class BookController {    /**     * 读取yml配置中的数据有三种方式     * 1.直接定义变量然后通过@Value注解赋值读取     * 2.通过Environment对象读取     * 3.通过封装实体类来读取     */    @Autowired    private Entries entries;    @GetMapping("/{id}")    public String queryById(@PathVariable Integer id) {        System.out.println(entries.getAge());        System.out.println(entries.getName());        System.out.println(entries.getSubject()[1]);        return "hello,SpringBoot!";    }}

结果如下:

  

4.补充:yml实现多环境配置的方式

项目中肯定会面临多环境的配置,比如生产环境测试环境和开发环境的端口都是不一样的,这个时候yml也支持多环境的简洁配置

spring:   profiles:    active: dev #这里就是选择当前使用哪个环境#多环境以---分隔即可---server:  port: 8081spring:  profiles: pro #生产环境---server:  port: 8083spring:  profiles: dev #开发环境---server:  port: 8084spring:  profiles: test #测试环境

上一篇:arcgis怎么创建服务器?

下一篇:怎么下载服务器里面的图片?