当前位置: 首页 > news >正文

SpringBoot解析指定Yaml配置文件

在这里插入图片描述
再来个文章目录

文章目录

  • 前言
  • 1、自定义配置文件
  • 2、配置对象类
  • 3、YamlPropertiesSourceFactory

下面还有投票,帮忙投个票👍

前言

最近在看某个开源项目代码并准备参与其中,代码过了一遍后发现多个自定义的配置文件用来装载业务配置代替数据库查询,直接响应给前端,这里简单记录一下实现过程。

我们通常在SpringBoot项目中用配置文件属性时使用@ConfigurationProperties或@Value默认配置文件的属性值,也就是application.yml或者application.properties文件中的属性值。

但是不能全都往默认配置文件里堆的,本文利用@PropertySource和@ConfigurationProperties注解引用其它配置文件的属性值。

1、自定义配置文件

在resources下创建my.yaml文件,“-”用来表示数组类型,一定要注意空格

my:contents:- id: 12121name: nadasd- id: 3333name: vfffff

2、配置对象类

创建配置类对象,在类上添加@Component、@PropertySource、@ConfigurationProperties注解。

@Component是将该类交由spring管理,@PropertySource用来指定配置文件及解析Yaml格式,@ConfigurationProperties是将解析后的配置文件属性自动注入该类的属性。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;@Component
@PropertySource(value = "classpath:my.yaml", factory = YamlPropertiesSourceFactory.class)
@ConfigurationProperties(prefix = "my")
public class MyProperties {private List<content> contents = new ArrayList<>();public List<content> getContents() {return contents;}public void setContents(List<content> contents) {this.contents = contents;}}class content {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

@PropertySource注解是Spring用于加载配置文件,@PropertySource属性如下:

  • name:默认为空,不指定Spring自动生成
  • value:配置文件
  • ignoreResourceNotFound:没有找到配置文件是否忽略,默认false,4.0版本加入
  • encoding:配置文件编码格式,默认UTF-8 4.3版本才加入
  • factory:配置文件解析工厂,默认:PropertySourceFactory.class 4.3版本才加入,如果是之前的版本就需要手动注入配置文件解析Bean

Spring Boot 默认不支持@PropertySource读取yaml 文件,需要自定义PropertySourceFactory进行解析。

3、YamlPropertiesSourceFactory

创建YamlPropertiesSourceFactory类用来解析Yaml格式的文件。

import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;import java.io.IOException;
import java.util.List;
import java.util.Optional;public class YamlPropertiesSourceFactory implements PropertySourceFactory {@Overridepublic PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {String resourceName = Optional.ofNullable(name).orElse(resource.getResource().getFilename());List<PropertySource<?>> yamlSources = new YamlPropertySourceLoader().load(resourceName, resource.getResource());return yamlSources.get(0);}}
http://www.lryc.cn/news/41479.html

相关文章:

  • C++基础算法③——排序算法(选择、冒泡附完整代码)
  • 《高质量C/C++编程》读书笔记一
  • 【完美解决】python flask如何直接加载html,css,js,image等下载的网页模板
  • 2023美赛C题【分析思路+代码】
  • 考研复试6 编译原理
  • uni-app:登录与支付--用户信息
  • Docker 部署 MySQL
  • 警惕,3月20日WOS目录更新,50本SCI/SSCI被剔除,这个出版社多达18本
  • 【 Linux入门 】之 手搓 命令行解释器 bash(带源码)
  • 【运维】运维常用命令
  • MYSQL常用命令大全
  • 锚点定位方案
  • Flink学习--第一章 初识Flink
  • 电脑技巧:常见的浏览器内核介绍
  • 【数据分析之道①】字符串
  • 网络安全之防火墙
  • STM32之点亮一个LED小灯(轮询法)
  • pandas读CSV、读JSON、Excel
  • 企业站项目
  • STM32开发(九)STM32F103 通信 —— I2C通信编程详解
  • 手撕数据结构—栈
  • 【java刷题】排序子序列
  • Springboot怎么快速集成Mybatis和thymeleaf?
  • shell常见面试题一
  • python如何快速采集美~女视频?无反爬
  • kali内置超好用的代理工具proxychains
  • Java栈和队列·下
  • b01lers CTF web 复现
  • 三月份跳槽了,历经字节测开岗4轮面试,不出意外,被刷了...
  • springboot+vue驾校管理系统 idea科目一四预约考试,练车