scala - scala 在运行时动态读取配置
问题描述
我在下面的文件夹中有一个配置文件
main
scala
resources
application.conf
并且包含
path{
http{
url = "http://testingurl"
}
}
我正在使用下面的代码阅读
import com.typesafe.config.Config;
val url = conf.getString("path.http.url")
我正在阅读构建时提供的静态信息。
现在我想在运行时阅读这些内容,即使在构建 jar 之后,用户也应该能够修改配置。
我的要求是在构建 jar 后修改 url 事件,我不想作为参数传递给 main 函数,因为我有很多这样的值需要在构建 jar 后修改
解决方案
请参阅: Lightbend 配置自述文件
在运行时使用:
-Dconfig.file=<relative or absolute path>
看:
For applications using application.{conf,json,properties},
system properties can be used to force a different config source
(e.g. from command line -Dconfig.file=path/to/config-file):
config.resource specifies a resource name - not a basename, i.e. application.conf not application
config.file specifies a filesystem path, again it should include the extension, not be a basename
config.url specifies a URL
These system properties specify a replacement for application.{conf,json,properties}, not an addition.
They only affect apps using the default ConfigFactory.load() configuration.
In the replacement config file, you can use include "application" to include the original default config file;
after the include statement you could go on to override certain settings.
推荐阅读
- php - 如何在 magento 2.x 中使用 TransportBuilder 发送 iCal 会议请求
- android - 屏幕尺寸无效?
- webpack - webpack 4 - 文件加载器不工作
- javascript - 在 JavaScript 中单击删除多个项目
- php - 与 Laravel Eloquent 类似的文章和排除结果
- java - 从redis转换json对象并从中提取List
- node.js - Nodejs代理不流式传输帖子数据
- sql-server - 如何使用 SQL Server 中的连接表更新更多条件?
- python - 使用搜索功能进行网页抓取并存储返回页面的结果
- json - 使用 TypeScript 在 Json 中解码 URL