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

03-29 6315阅读 0评论

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

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

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!";
    }
}

结果如下:

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

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!";
    }
}

结果如下:

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

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!";
    }
}

结果如下:

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

以上就是我们常用的三种读取配置类中数据的方式。

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 #测试环境

免责声明
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明。
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所
提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何
损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在
转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并白负版权等法律责任。

手机扫描二维码访问

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,6315人围观)

还没有评论,来说两句吧...

目录[+]