java - 如何在 Spring 框架之外加载 Spring YAML 配置?
问题描述
我有一个 Spring Boot 应用程序,它在 application.yml 配置文件中定义了一些端点。YAML 包含多个文档,每个配置文件一个。
我想在启动应用程序之前运行 Gradle 任务。Gradle 任务将配置文件作为参数传递。它应该使用该配置文件的配置中定义的端点。
如何在不加载整个 Spring 框架的情况下从配置中加载值?
例子
# src/main/resources/application.yml
endpoints:
foo: localhost:12345/foo
bar: localhost:12345/bar
---
spring.profiles: staging
endpoints:
foo: dev.foo.com/api
bar: dev.bar.com/api
---
spring.profiles: production
endpoints:
foo: foo.com/api
bar: bar.com/api
public class Task {
public static void main(String[] args) {
String profile = arg[0];
// TODO load endpoints from src/main/resources/application.yml
Map<String, Object> endpoints = ...
System.out.println(endpoints.get("foo");
}
}
解决方案
推荐阅读
- ruby-on-rails - 工作结束后,工作人员中的内存未释放
- java - 如何使用 Java 将 XML 文档转换为目标 XML 模板结构(减少属性/元素)
- r - 按日期范围设置不平衡面板数据
- android - 向广播接收器发送隐式意图
- android - 解决。在 AlertDialog 中显示 MapView 时,对话框按钮消失
- android - 如何在颤动的谷歌地图中添加自定义大小?
- nlp - 如何识别作为光学字符识别 (OCR) 输出的文本中的实体?
- regex - Slash doesn't work in matching query using regexp in Elasticsearch
- swift - Xcode 项目在版本 10.1 上启动项目后抛出 NSUnknownKeyException
- bash - 如何计算文件中给定单词的出现次数?