简体   繁体   English

如何在运行时刷新application.conf

[英]how to refresh application.conf at runtime

I update a key value in application.conf by setting the environment variable 我通过设置环境变量来更新application.conf中的键值

play.http.secret.key=${?MY_SECRET_KEY}

But it still loads the previous value. 但是它仍然加载先前的值。
After a system reboot the change takes effect. 系统重新引导后,更改生效。
Is there a way to refresh the config file without rebooting? 有没有一种方法可以在不重新启动的情况下刷新配置文件?

AFAIK, there is no Out of the box mechanism to do such thing. AFAIK,没有开箱即用的机制可以做这种事情。 As it turns out, when you start Play through the "sbt run" command, it starts in Dev mode, creating two ClassLoaders, one for your code, and the other one for the immutable code, like libraries, dependencies, and the framework code itself. 事实证明,通过“ sbt run”命令启动Play时,它将以Dev模式启动,创建两个ClassLoader,一个用于您的代码,另一个用于不可变的代码,例如库,依赖项和框架代码本身。 This is done this way as to provide the hot deploy feature, killing and reloading the first CL. 这样做是为了提供热部署功能,即杀死并重新加载第一个CL。 Because the application.conf is loaded by Play on start up, I would think that it is loaded within the fixed ClassLoader, hence no reload is possible. 因为application.conf是在启动时由Play加载的,所以我认为它是在固定的ClassLoader中加载的,因此无法重新加载。

Try the following: 请尝试以下操作:

Given in a file called sample1.conf : 在名为sample1.conf的文件中给出:

a {
   b {
      c = 30
      d = ["Red", "Orange", "Green", "Blue"]
   }
}

If you wish to change a property be sure to change it as system property first and call invalidate caches then load again. 如果要更改属性,请确保先将其更改为系统属性,然后调用无效缓存,然后再次加载。 This is also what allows you to override on the command line. 这也是允许您在命令行上覆盖的内容。

System.setProperty("a.b.c", "100")
ConfigFactory.invalidateCaches()
val config = ConfigFactory.load("sample1")
config.getDouble("a.b.c") should be (100.0)

Don't know if this would work in all scenarios. 不知道这是否适用于所有情况。 So it may or may not work with your application of choice. 因此,它可能会或可能不会与您选择的应用程序一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM