gpt4 book ai didi

scala - 如何在Scala的TypeSafe配置中读取环境变量?

转载 作者:行者123 更新时间:2023-12-01 09:16:36 25 4
gpt4 key购买 nike

我正在尝试读取 Scala 中 typesafe config 中的环境变量,配置光滑的数据库。这是我尝试过的

remote_test_db = {
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
properties = {
driver = "org.postgresql.Driver"
url = ${?REMOTE_TEST_DB_URL}
user = ${?REMOTE_TEST_DB_USERNAME}
password = ${?REMOTE_TEST_DB_PASSWORD}
}
connectionPool = disabled
keepAliveConnection = true
}

但我收到此错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.flyhomes.mls_pull.dump.MlsProvider.main(MlsProvider.scala)
Caused by: com.typesafe.config.ConfigException$NotResolved: need to Config#resolve(), see the API docs for Config#resolve(); substitution not resolved: ConfigConcatenation(${?REMOTE_TEST_DB_USERNAME})
at com.typesafe.config.impl.ConfigConcatenation.notResolved(ConfigConcatenation.java:51)
at com.typesafe.config.impl.ConfigConcatenation.valueType(ConfigConcatenation.java:58)
at slick.util.ConfigExtensionMethods$$anonfun$slick$util$ConfigExtensionMethods$$toProps$1$1.apply(GlobalConfig.scala:71)
at slick.util.ConfigExtensionMethods$$anonfun$slick$util$ConfigExtensionMethods$$toProps$1$1.apply(GlobalConfig.scala:69)
at scala.collection.Iterator$class.foreach(Iterator.scala:893)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1336)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at slick.util.ConfigExtensionMethods$.slick$util$ConfigExtensionMethods$$toProps$1(GlobalConfig.scala:69)
at slick.util.ConfigExtensionMethods$.toProperties$extension(GlobalConfig.scala:78)
at slick.util.ConfigExtensionMethods$.getPropertiesOr$extension(GlobalConfig.scala:64)
at slick.util.ConfigExtensionMethods$.getPropertiesOpt$extension(GlobalConfig.scala:84)
at slick.jdbc.DataSourceJdbcDataSource$.forConfig(JdbcDataSource.scala:90)
at slick.jdbc.DataSourceJdbcDataSource$.forConfig(JdbcDataSource.scala:86)
at slick.jdbc.JdbcDataSource$.forConfig(JdbcDataSource.scala:48)
at slick.jdbc.JdbcBackend$DatabaseFactoryDef$class.forConfig(JdbcBackend.scala:288)
at slick.jdbc.JdbcBackend$$anon$3.forConfig(JdbcBackend.scala:33)
at com.flyhomes.mls_pull.Databases$.remoteTestDb$lzycompute(Databases.scala:21)
at com.flyhomes.mls_pull.Databases$.remoteTestDb(Databases.scala:21)
at com.flyhomes.mls_pull.dump.MlsProvider$.<init>(MlsProvider.scala:18)
at com.flyhomes.mls_pull.dump.MlsProvider$.<clinit>(MlsProvider.scala)
... 1 more

如何在此处访问环境变量?

最佳答案

从异常(exception)情况来看,您似乎需要调用 resolve - 第 3 行:need to Config#resolve() .这实际上将替换每个变量。也许这会有所帮助:

val config = ??? // load config here
config.resolve() // force the substitution of variables

引用自该类(class)的文档:

解析替换

替换是配置文件中的 ${foo.bar} 语法,在规范中有描述。解析替换将这些引用替换为实际值。

在使用 Config 之前,有必要调用 resolve() 来处理替换(尽管 ConfigFactory.load() 和类似的方法已经为您完成了解析)。

关于scala - 如何在Scala的TypeSafe配置中读取环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43086926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com