我正在使用ficus和typesafe配置来管理配置。
我想在这个项目中使用Intellij的scala工作表,但是当我尝试以下代码时:
import what.ever.ApplicationSetting
ApplicationSetting.aws.accessKey
但是我收到以下错误:
java.lang.ExceptionInInitializerError
at some.thing.A$A11$A$A11.get$$instance$$res0(testRes.sc:3)
at #worksheet#.#worksheet#(testRes.sc:11)
Caused by: com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'aws'
at com.typesafe.config.impl.SimpleConfig.findKey(SimpleConfig.java:124)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:147)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:159)
at com.typesafe.config.impl.SimpleConfig.find(SimpleConfig.java:164)
at com.typesafe.config.impl.SimpleConfig.getString(SimpleConfig.java:206)
at net.ceedubs.ficus.readers.StringReader$$anon$1.read(StringReader.scala:7)
at net.ceedubs.ficus.readers.StringReader$$anon$1.read(StringReader.scala:6)
at what.ever.ApplicationSetting$$anon$1.read(ApplicationSetting.scala:24)
application.conf
的内容如下:
package what.ever
import com.typesafe.config.ConfigFactory
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
object ApplicationSetting {
val env = sys.env.getOrElse("DEV_ENV", "default")
val config = {
ConfigFactory.defaultOverrides
.withFallback(ConfigFactory.load(env))
.withFallback(ConfigFactory.load)
}
case class AWS(accessKey: String,
secretKey: String)
val aws = config.as[AWS]("aws")
}
我发现它很奇怪,因为相同的代码在scala控制台中起作用。
我很感激任何建议。
如果您想测试代码结帐repo。
答案 0 :(得分:2)
我发现的一种解决方法是以另一种方式加载配置文件。
首先将配置文件更改为myAppConf.conf
,这是为了避免配置文件从合并策略中消失。
package what.ever
import java.io.File
import com.typesafe.config.ConfigFactory
import net.ceedubs.ficus.Ficus._
import net.ceedubs.ficus.readers.ArbitraryTypeReader._
object ApplicationSetting {
val confPath = getClass.getResource("/myAppConf.conf")
val config = ConfigFactory.parseFile(new File(confPath.getPath)).resolve()
case class AWS(accessKey: String,
secretKey: String)
val aws = config.as[AWS]("aws")
}