我正在尝试使用类型安全的配置读取以下配置文件
common = {
jdbcDriver = "com.mysql.jdbc.Driver"
slickDriver = "slick.driver.MySQLDriver"
port = 3306
db = "foo"
user = "bar"
password = "baz"
}
source = ${common} {server = "remoteserver"}
target = ${common} {server = "localserver"}
当我尝试使用此代码读取我的配置
val conf = ConfigFactory.parseFile(new File("src/main/resources/application.conf"))
val username = conf.getString("source.user")
我收到错误
com.typesafe.config.ConfigException$NotResolved: source.user has not been resolved, you need to call Config#resolve(), see API docs for Config#resolve()
如果我将所有内容都放在"源"或"目标"标签中,我不会收到任何错误。只有当我尝试使用"common"时,我才会收到错误
我自己解决了。
ConfigFactory.parseFile(new File("src/main/resources/application.conf")).resolve()
我解决了。
Config confSwitchEnv = ConfigFactory.load("env.conf");
env.conf
位于resources
目录中。
参考: https://nicedoc.io/lightbend/config