[英]How to read environment variables in TypeSafe config in scala?
I am trying to read the environment variables in typesafe config in scala, configuring the slick database. 我试图在scala中的typesafe配置中读取环境变量,配置光滑的数据库。 Here is what I tried 这是我试过的
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
}
But I get this error: 但我得到这个错误:
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
How do I access the environment variables here? 如何在此处访问环境变量?
From the exception it seems that you need to call resolve - line 3: need to Config#resolve()
. 从例外情况看,您需要调用resolve - 第3行: need to Config#resolve()
。 This will actually substitute every variable. 这实际上将替换每个变量。 Maybe this will help: 也许这会有所帮助:
val config = ??? // load config here
config.resolve() // force the substitution of variables
Quoted from the docs of the class: 引用该课程的文档:
Resolving substitutions 解决替换
Substitutions are the ${foo.bar} syntax in config files, described in the specification. 替换是配置文件中的$ {foo.bar}语法,在规范中描述。 Resolving substitutions replaces these references with real values. 解析替换用实际值替换这些引用。
Before using a Config it's necessary to call resolve() to handle substitutions (though ConfigFactory.load() and similar methods will do the resolve for you already). 在使用Config之前,必须调用resolve()来处理替换(尽管ConfigFactory.load()和类似的方法已经为你做了解决)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.