2016-08-02 31 views
2

我正在使用ficus和typesafe配置來管理配置。Typesafe配置庫在Intellij Scala工作表中不可用

我想使用的IntelliJ的斯卡拉工作在這個項目中,但是當我嘗試下面的代碼:

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

回答

1

我發現的解決方法是以另一種方式加載配置文件。 首先將配置文件更改爲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") 

} 
+1

我不能幫上什麼忙,但有時當我遇到麻煩的IntelliJ中的工作表我去REPL或[菊](http://www.lihaoyi.com/Ammonite/#Ammonite-REPL)後者提供了很好的增強repl和簡單的方法來加載庫。 – Barry

+1

亞捫人仍然難以添加類路徑。所以在某些情況下,它不能成爲替代品。雖然repl是好的。 – Vasco