如何使用typesafe配置库只渲染文件内容?

时间:2017-08-30 15:19:25

标签: scala typesafe-config

我使用myConfig.root().render(ConfigRenderOptions.concise().setFormatted(true)))打印我的配置内容。但我发现它包含许多其他信息,例如"version" : "2.4.16""stdout-loglevel" : "WARNING"等,这些信息未在我的内容中定义配置文件 信息来自哪里?
我怎样才能打印配置文件内容?

3 个答案:

答案 0 :(得分:1)

我使用parseResourcesAnySyntax方式通过myConfig方法使用default呈现配置:

object DataServiceConfig {
  val local = ConfigFactory.parseResourcesAnySyntax("local")
  val online = ConfigFactory.parseResourcesAnySyntax("online")
  val develop = ConfigFactory.parseResourcesAnySyntax("application") //develop environment
  val default = ConfigFactory.load("application") //default environment

  val myConfig = local.withFallback(online).withFallback(develop)
  val combinedConfig = myConfig.withFallback(default)

  def printConf(config: Config): Unit = println(config.root().render(ConfigRenderOptions.concise().setFormatted(true).setJson(true)))


}

print config:DataServiceConfig.printConf(DataServiceConfig.myConfig)

答案 1 :(得分:0)

您可能正在使用Akka 2.4.16(直接或间接),在这种情况下,"额外"配置设置是从reference.conf中提取的,如documentation中所述。 reference.conf包含所有默认配置设置,application.conf可以覆盖任何这些设置。

ActorSystemreference.confapplication.conf合并,如here所示。我不认为通过Typesafe配置API可以呈现application.conf的内容而不包括reference.conf的合并设置。

答案 2 :(得分:0)

您可以使用java.util.Properties来获取文件内容,如下所示:

def getPropByFileName(fileName: String) = {
    val inputStream = this.getClass.getClassLoader.getResourceAsStream(fileName)
    val props = new Properties()
    props.load(inputStream)
    props
}
相关问题