简体   繁体   中英

Typesafe Config Library unusable in Intellij Scala worksheet

I am using ficus and typesafe config to manage configurations.

I want to use Intellij's scala worksheet in this project but when I try the following code:

import what.ever.ApplicationSetting

ApplicationSetting.aws.accessKey

However I get the following error:

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)

The content of application.conf is the following:

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")

}

I find it strange because the same code did work in the scala console.

I would appreciate any suggestion.

In case you want to test the code checkout this repo .

A workaround I found was to load the configuration file in another way. First change the configuration file to something like myAppConf.conf this is to avoid configuration files disappearing from merge strategies.

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")

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM