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

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

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: 8081
spring:
  profiles: pro #生产环境

---

server:
  port: 8083
spring:
  profiles: dev #开发环境


---

server:
  port: 8084
spring:
  profiles: test #测试环境

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值