一、SpringBoot 读取 YML 配置文件
Spring Framework 是一个轻量级的IoC和AOP容器框架,SpringBoot 扩展了 Spring 的理念,并做出良好的封装。它是针对 Spring 应用程序开发的框架,以便快速轻松地创建基于 Spring 的生产级别的应用程序。因为 SpringBoot 对 Spring 进行了封装,所以我们不需要去配置大量繁琐的 XML 文件。
在 SpringBoot 中,我们可以通过使用 @ConfigurationProperties 来绑定一个特定的属性类,使得它可以自动读取 application.yml 文件中的配置,使用方法如下:
# application.yml
server:
port: 8080
context-path: /
servlet:
context-path: /
custom:
field1: value1
field2: value2
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("custom")
public class CustomProperties {
private String field1;
private String field2;
// getter and setter methods
}
在上述示例中,我们定义了一个名为 CustomProperties 的属性类,并使用 @ConfigurationProperties(“custom”) 注解绑定了一个前缀为 custom 的属性配置。这样,我们直接在该类中定义需要使用的属性字段,并通过 setter 方法自动完成绑定。
二、Jar 包读取外部 YML 配置文件
有时候,在部署 jar 包的过程中,我们可能需要读取 jar 包外部的 YML 配置文件。在 Java 中,可以使用 Properties 进行配置文件的读取,但是 Properties 并不支持 YML 文件格式,所以我们需要引入相应的依赖。
org.yaml
snakeyaml
1.27
在引入依赖之后,我们就可以通过如下代码来读取外部 YML 配置文件:
public class ExternalConfigReader {
public static void main(String[] args) {
InputStream inputStream = null;
try {
String filePath = "file:/opt/config/config.yml";
URL url = new URL(filePath);
inputStream = url.openStream();
// 解析YML文件
Yaml yaml = new Yaml();
Map map = (Map) yaml.load(inputStream);
// 输出数据
System.out.println(map.get("field1"));
System.out.println(map.get("field2"));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
在上述示例中,我们读取了/opt/config/config.yml 文件,并使用 SnakeYaml 完成了对 YML 文件的读取。接下来,我们可以通过 Map 对象获取相应的属性值。
三、如何读取 YML 配置文件
除了 SpringBoot 和 Jar 包外部配置,我们还可以使用其他的方式来读取 YML 配置文件。在 Java 中,通常会使用如下的两种方式:
1. Properties 实现
Properties 是 Java I/O 中与关联文件进行交互的类,主要用于读取和写入属性文件。使用 Properties 读取 YML 文件需要先将 YML 文件转换成 Properties 格式,其格式与 YML 类似,只需要将 ‘:’ 替换为 ‘=’ 即可。
# config.yml field1: value1 field2: value2
// 读取 YML 文件
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("config.yml");
properties.load(new InputStreamReader(inputStream, "UTF-8"));
// 输出数据
System.out.println(properties.getProperty("field1"));
System.out.println(properties.getProperty("field2"));
2. SnakeYaml 实现
SnakeYaml 是一款处理 YAML 格式文件的 Java 库,可用于生成 Java 对象、解析 YAML 文件、绑定 Properties 等操作。使用 SnakeYaml,我们可以直接读取 YML 配置文件并返回一个 Map 对象。
// 读取 YML 文件
Yaml yaml = new Yaml();
InputStream inputStream = new FileInputStream("config.yml");
Map map = (Map) yaml.load(inputStream);
// 输出数据
System.out.println(map.get("field1"));
System.out.println(map.get("field2"));
四、SpringBoot 读取外部 YML 配置文件
在 SpringBoot 中,我们可以通过在 application.yml 文件中添加外部配置文件路径来读取外部 YML 配置文件:
# application.yml
spring:
profiles:
active: prod
config:
location: file:/opt/config/
custom:
field1: value1
field2: value2
在上述示例中,我们通过在 application.yml 中配置了外部配置文件的路径 file:/opt/config/,SpringBoot 在启动时会读取该路径下的配置文件。接下来,我们可以通过将该路径下的配置文件转换成一个属性类来获取相应的属性值。具体示例可参考第一部分中的 SpringBoot 读取 YML 配置文件部分。
原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/186604.html
微信扫一扫
支付宝扫一扫