如何在Scala工作表和Akka中引用配置文件?

如何在Scala工作表和Akka中引用配置文件?,scala,akka,config,worksheet,Scala,Akka,Config,Worksheet,我试图在Scala工作表中创建一个简单的akka系统,但每次尝试时,都会出现这个错误 com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka' at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(tsets.sc:148) at com.typesafe.config.impl.SimpleConfig.findKe

我试图在Scala工作表中创建一个简单的akka系统,但每次尝试时,都会出现这个错误

com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'akka'
at com.typesafe.config.impl.SimpleConfig.findKeyOrNull(tsets.sc:148)
at com.typesafe.config.impl.SimpleConfig.findKey(tsets.sc:141)
at com.typesafe.config.impl.SimpleConfig.findOrNull(tsets.sc:168)
at com.typesafe.config.impl.SimpleConfig.find(tsets.sc:180)
at com.typesafe.config.impl.SimpleConfig.find(tsets.sc:185)
at com.typesafe.config.impl.SimpleConfig.getString(tsets.sc:242)
at akka.actor.ActorSystem$Settings.<init>(tsets.sc:166)
at akka.actor.ActorSystemImpl.<init>(tsets.sc:533)
at akka.actor.ActorSystem$.apply(tsets.sc:139)
at akka.actor.ActorSystem$.apply(tsets.sc:116)
at #worksheet#.system$lzycompute(tsets.sc:9)
at #worksheet#.system(tsets.sc:9)
at #worksheet#.get$$instance$$system(tsets.sc:9)
at A$A9$.main(tsets.sc:35)
at #worksheet#.#worksheet#(tsets.sc)
我甚至尝试将此添加到build.sbt

assemblyMergeStrategy in assembly := {
  case PathList("application.conf") => MergeStrategy.concat
}

其中一个可能的变体是使用
resolve
调用
ConfigFactory.parseFile
(如果配置文件中有一些替换,如
receive buffer size=${send buffer size}
):

你的
application.conf
文件应该在
resources
文件夹中,你的工作表可以放在
scala
文件夹中

更新

如果你使用Intellij IDEA。这是有道理的

  • 转到文件>设置>语言和框架>Scala>工作表
  • 取消勾选“在编译器进程中运行配置”,然后按“确定”
  • 因此,您可以自由使用
    ConfigFactory.load()


    我在2018.1.5版中使用了IDEA,您是否已将
    应用程序.conf
    放入
    资源文件夹
    ?application.conf和工作表都在资源文件夹中
    应用程序.conf
    文件在
    资源文件夹中。在使用
    scala
    文件夹中的工作表执行此操作之前,我已经尝试过了,但得到了相同的错误。我不理解使用
    resolve
    可能的变量的重要性。它在做什么?
    resolve
    ,当您需要解析配置文件中的替换时。假设您在使用以下语法将同一配置中的另一个变量的变量值指定给该变量时,发生了错误
    receive buffer size=${send buffer size}
    。否则,您将在
    ConfigFactory.load()
    中获得
    ConfigExceptionNotResolved
    ,它将自动调用,但在这种情况下,如果您有一些带有
    someVariable=${anotherVariable}
    语法的子替换,则需要调用
    resolve
    ,谢谢。更改工作表设置对我很有用。
    assemblyMergeStrategy in assembly := {
      case PathList("application.conf") => MergeStrategy.concat
    }
    
    val confPath = getClass.getResource("/application.conf")
    val config = ConfigFactory.parseFile(new File(confPath.getPath)).resolve()
    config.getString("akka.loglevel")