简体   繁体   English

在应用程序运行时更新 Spring Boot 属性的更好方法

[英]Better way to update Spring Boot properties while app is running

So far, I have seen methods to update a Spring boot application, by changing the property file itself, then reloading the file, or by using @RefreshScope annotation, or by Spring Cloud Config.到目前为止,我已经看到了更新 Spring 启动应用程序的方法,通过更改属性文件本身,然后重新加载文件,或者使用@RefreshScope注释,或者通过 Spring Cloud Config。

I was not able to find anything straight forward (something like getEnvironment().updateProperty(key, value) ).我无法直接找到任何东西(例如getEnvironment().updateProperty(key, value) )。

Is there such a way to do that?有没有办法做到这一点?

For example, I need to change the logging type of my application, to show the SQL;例如,我需要更改应用程序的日志记录类型,以显示 SQL; My application.properties file contains this line: spring.jpa.show-sql=false .我的application.properties文件包含这一行: spring.jpa.show-sql=false

But what if I want to set this to true while the application is running, through a REST call?但是,如果我想在应用程序运行时通过 REST 调用将其设置为true ,该怎么办?

Can you suggest a simpler way to do this?你能建议一个更简单的方法来做到这一点吗? And/Or show examples of how you would do it yourself, for this specific property?和/或展示您如何为这个特定属性自己做的例子?

EDIT: The changes do not need to be reflected in the application.properties file.编辑:更改不需要反映在application.properties文件中。 So when the application is restarted, it would use the properties that were initially set in the application.properties file.因此,当应用程序重新启动时,它将使用最初在application.properties文件中设置的属性。

Some answers recommends Spring Cloud Config Server.一些答案推荐 Spring Cloud Config Server。 It may be an overkill for some.对某些人来说,这可能是矫枉过正。 This SO answer is exactly what you are looking for.这个 SO 答案正是您正在寻找的。 How to hot reload properties in Spring Boot and Java EE如何在 Spring Boot 和 Java EE 中热重载属性

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

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