好记性不如烂笔头~
typesafe.config
如何读取多个配置文件并将多个配置文件内容汇总到一个config对象中
def config(configFiles): Config = {
val config_ = null
if (config_ == null) {
config_ = ConfigFactory.load
configFiles.foreach { fileName =>
val file = new File(fileName)
config_ = config_.withFallback(ConfigFactory.parseFile(file).resolve())
}
}
config_
}
val configFiles = List(conf1, conf2, conf3)
val conf = config(configFiles)
typesafe.config
如何读配置文件中的嵌套字典
现有json配置文件如下:
{
"path": "/some/path"
"models": {
"model1": "/path/to/m1",
"model2": "/path/to/m2",
}
}
如何读取配置文件内容以及遍历内层models呢?
import com.typesafe.config.Config
val config = ConfigFactory.load(filename)
// 读取path变量
val path = config.getString("path")
// 将models变量读进来并转为map
val mods = config.getConfig("models")
val models = mods.root.keySet.asScala.map(key => key -> mods.getString(key)).toMap
// 另一种方法
import java.util
val models = config.getObject("models").unwrapped().asInstanceOf[util.Map[String, String]].asScala
参考:https://stackoverflow.com/questions/56581221/typesafe-config-get-as-map
网友评论