【问题标题】:scala read the configs dynamically during run timescala 在运行时动态读取配置
【发布时间】:2018-10-27 11:42:35
【问题描述】:

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

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 后修改

【问题讨论】:

    标签: scala maven config


    【解决方案1】:

    见: Lightbend Config Readme

    在运行时使用: -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.
    

    【讨论】:

      【解决方案2】:

      我假设 .jar 文件是使用作为

      提供的密钥构建的

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

      您每次都将使用修改后的 .config 文件运行此 .jar

      我的要求是构建jar后修改url事件,

      一种可能的解决方案是提供一个配置值数组,其中 .jar 文件中的键保持不变

      import com.typesafe.config.ConfigFactory
      
      ConfigFactory.load().getStringList("url.key").stream().map(eachUrl => functionToBeCalled)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 2012-08-02
        • 2019-11-17
        • 1970-01-01
        相关资源
        最近更新 更多