13

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.

I was not able to find anything straight forward (something like 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; My application.properties file contains this line: spring.jpa.show-sql=false.

But what if I want to set this to true while the application is running, through a REST call?

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. So when the application is restarted, it would use the properties that were initially set in the application.properties file.

Wilhelm Sorban
  • 1,012
  • 1
  • 17
  • 37
  • 2
    There isn't. Properties are applied at load-time... Changing them requires a restart or recreation of beans (hence the `@RefreshScope`). You might be able to use JMX to change some properties in a running application but this won't be reflected in your `application.properties`. – M. Deinum Aug 29 '17 at 13:39
  • Yes, thanks, the properties do not need to be reflected in the properties file. They just need to be applied to the current running instance. – Wilhelm Sorban Aug 29 '17 at 13:43
  • 2
    You can with Spring-Boot Cloud. See https://stackoverflow.com/questions/26717187/refreshing-spring-boot-properties#26717273 – gtonic Aug 29 '17 at 13:44
  • Possible duplicate of [refreshing spring boot properties](https://stackoverflow.com/questions/26717187/refreshing-spring-boot-properties) – gtonic Aug 29 '17 at 13:44
  • @gtonic I would like to see, if possible, specific examples for the property described in my post, which keep things as simple as possible. – Wilhelm Sorban Aug 29 '17 at 13:49
  • I've done this using [this answer](https://stackoverflow.com/questions/40287771/how-to-reload-a-value-property-from-application-properties-in-spring/40288822#40288822) – Essex Boy Aug 29 '17 at 14:05
  • @Essex Boy This still requires changing the the application.properties file, which is something I would like to avoid. – Wilhelm Sorban Aug 31 '17 at 07:58
  • If you want to change a property you're going to have to tell it what the new property is somehow!! – Essex Boy Aug 31 '17 at 08:01
  • As far as I know, you can set properties programatically. So it would require setting/resetting that property programatically, then refreshing the application scope. – Wilhelm Sorban Aug 31 '17 at 08:04
  • This answer exactly what you are looking for https://stackoverflow.com/a/52648630/39998 – David Hofmann Oct 04 '18 at 14:24
  • I would go with Consul KV, instead of any custom implementation, as the third party would have been thoroughly tested and provide more features. – Bhaumik Thakkar Aug 22 '23 at 06:12

3 Answers3

1

You can use kubernetes env like below:

spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: SPRING_PROFILES_ACTIVE
      value: "prod"

If not use kubernetes, you can use database(I prefer NoSql for this solution) to changing your properties. write schedule for getting data from your database and update your properties

ali
  • 21
  • 6
0

Some answers recommends Spring Cloud Config Server. It may be an overkill for some. This SO answer is exactly what you are looking for. How to hot reload properties in Spring Boot and Java EE

David Hofmann
  • 5,683
  • 12
  • 50
  • 78
0

If an application has special requirements regarding its availability, it is probably desired that changes on configuration files can be done without the need for a restart. The application should automatically detect such changes an react accordingly. This feature is referred to as automatic reloading.

The reloading mechanism defined by Apache Commons Configuration library which has multiple components you can use for this approach. you can learn about how to use it in this link https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html