合并多个TypeSafe Config文件并仅在它们全部合并后再解析 [英] Merging multiple TypeSafe Config files and resolving only after they are all merged
问题描述
我正在编写测试代码以验证RESTful服务.我希望能够通过在执行测试之前简单地更改环境变量来将其指向我们的任何不同环境.
I am writing test code to validate a RESTful service. I want to be able to point it at any of our different environments by simply changing an environment variable before executing the tests.
我希望能够合并三个不同的配置文件:
I want to be able to merge three different config files:
-
conf/env/default.conf
-默认配置值 适用于所有环境 -
conf/env/<env>.conf
-特定于环境 值 -
application.conf
-用户对任何 以上
conf/env/default.conf
- the default configuration values for all environmentsconf/env/<env>.conf
- the environment-specific valuesapplication.conf
- the user's overrides of any of the above
我的想法是我不希望所有内容都放在一个配置文件中,并且冒着编辑不当导致配置项丢失的风险.因此,相反,请将它们分开并赋予用户覆盖它们的能力.
The idea is that I don't want everything in a single config file, and run the risk of a bad edit causing configuration items to get lost. So instead, keep them separate and give the user the ability to override them.
这是棘手的地方:default.conf
将包含$ {references}表示要在<env>.conf
中被覆盖的内容,并可能在application.conf
中被进一步覆盖.
Here's where it gets tricky: default.conf
will include ${references} to things that are meant to be overridden in <env>.conf
, and may be further overridden in application.conf
.
我需要推迟解决所有三个问题.我该怎么办?
I need to postpone resolving until all three are merged. How do I do that?
推荐答案
答案是使用ConfigFactory.parseResource()
代替ConfigFactory.load()
.
这是完成的结果
private lazy val defaultConfig = ConfigFactory.parseResources("conf/env/default.conf")
private lazy val environmentConfig = ConfigFactory.parseResources("conf/env/" + env + ".conf" )
private lazy val userConfig = ConfigFactory.parseResources("application.conf")
private lazy val config = ConfigFactory.load()
.withFallback(userConfig)
.withFallback(environmentConfig)
.withFallback(defaultConfig)
.resolve()
这篇关于合并多个TypeSafe Config文件并仅在它们全部合并后再解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!