Hi,
Lets say, have a list of conf files which are needed to load into the
ConfigFactory.load() and all the config files values need to be used in my application.
val value1 = config.getString(“key1”)
For example: There are three config files and from each file key and values needed to as part of the application program.
var fileNameArray: Array[String] = “mysql,hbase,mongodb”.split(",")
fileNameArray = fileNameArray.map(e => e + “.conf”)
for (eachfile <- fileNameArray) {
var curFileIndex = eachfile
println("curFileIndex—> "+ curFileIndex)
var curConfig = ConfigFactory.load(curFileIndex)
config = config.withFallback(curConfig)
}
Please can anyone of you suggest how to get the all key and values loaded into a single config Object by using the ConfigFactory.load() API.
If you only know the config files names at runtime, loading them separately and merging them with Config#withFallback
seems quite reasonable to me. You can do that elegantly with a fold
, for example:
val configNames = Seq("file_1.conf", "file_2.conf", "file_3.conf")
val mergedConfig = configNames.foldLeft(ConfigFactory.empty()) { (acc, configName) =>
val config = ConfigFactory.load(configName)
acc.withFallback(config)
}
(Not tested, but it should work…)
But otherwise a simpler way is to just include all config files in a main config file, typically application.conf
, and juste load that one.
Content of application.conf
:
include "file_1.conf"
include "file_2.conf"
include "file_3.conf"
And just load it with ConfigFactory.load()
. Not that if a key is duplicated in all 3 files, the value from file_3.conf
will be used.
1 Like
@guilgaly, Thank you It worked perfectly.