scala 在 运行 时间内动态读取配置
scala read the configs dynamically during run time
我在下面的文件夹中有一个配置文件减去
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 事件,我不想作为参数传递给主函数,因为我有太多这样的值需要在构建 jar 后修改
运行时使用:
-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.
我假设 .jar 文件是使用
提供的密钥构建的
val url = conf.getString("path.http.url")
并且每次使用修改后的 .config
文件
都会 运行 这个 .jar
my requirement is to modify url event after jar is build,
一个可能的解决方案是提供一组配置值,其中的键在 .jar 文件中保持不变
import com.typesafe.config.ConfigFactory
ConfigFactory.load().getStringList("url.key").stream().map(eachUrl => functionToBeCalled)
我在下面的文件夹中有一个配置文件减去
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 事件,我不想作为参数传递给主函数,因为我有太多这样的值需要在构建 jar 后修改
运行时使用:
-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.
我假设 .jar 文件是使用
提供的密钥构建的val url = conf.getString("path.http.url")
并且每次使用修改后的 .config
文件
my requirement is to modify url event after jar is build,
一个可能的解决方案是提供一组配置值,其中的键在 .jar 文件中保持不变
import com.typesafe.config.ConfigFactory
ConfigFactory.load().getStringList("url.key").stream().map(eachUrl => functionToBeCalled)