首页 > 解决方案 > scala 在运行时动态读取配置

问题描述

我在下面的文件夹中有一个配置文件

main
    scala
    resources
             application.conf

并且包含

path{
  http{
      url = "http://testingurl"

  }
}

我正在使用下面的代码阅读

import com.typesafe.config.Config;

val url = conf.getString("path.http.url")

我正在阅读构建时提供的静态信息。

现在我想在运行时阅读这些内容,即使在构建 jar 之后,用户也应该能够修改配置。

我的要求是在构建 jar 后修改 url 事件,我不想作为参数传递给 main 函数,因为我有很多这样的值需要在构建 jar 后修改

标签: scalamavenconfig

解决方案


请参阅: Lightbend 配置自述文件

在运行时使用: -Dconfig.file=<relative or absolute path>

看:

For applications using application.{conf,json,properties}, 
system properties can be used to force a different config source 
    (e.g. from command line -Dconfig.file=path/to/config-file):

    config.resource specifies a resource name - not a basename, i.e. application.conf not application
    config.file specifies a filesystem path, again it should include the extension, not be a basename
    config.url specifies a URL

These system properties specify a replacement for application.{conf,json,properties}, not an addition. 
They only affect apps using the default ConfigFactory.load() configuration. 
In the replacement config file, you can use include "application" to include the original default config file; 
after the include statement you could go on to override certain settings.

推荐阅读