Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update configuration of SpringBoot application at runtime without reloading whole ApplicationContext

I am trying to figure out how can I dynamically update/reload externalized configuration in a Spring Boot application without restarting the whole application.

Most of the advice involves reloading ApplicationContext after changing the externalized configuration, but that is equivalent to restarting the entire application, so this is not really all that useful.

Reading through SpringBoot reference documentation, I found a chapter 23.7 Typesafe Configuration Properties.

If I understand it correctly, this allows to define simple POJO classes that will hold your application (externalized) configuration values as attributes.

In theory at least, this scheme could be used to bind beans only once to the required configuration POJO and upon configuration change just update the values in the POJO. Components could easily pick up the changes next time they access getters on the POJO...

However, I have yet not managed to figure out how to enable this type of behavior. Is there some glaringly obvious way to dynamically update components annotated with @ConfigurationProperties when relevant configuration has changed?

like image 911
Roland Tepp Avatar asked Oct 27 '15 10:10

Roland Tepp


People also ask

How do you reload the configuration properties without restarting the spring boot app?

include=refresh: this is actuator property which will help us to refresh the properties without restarting the server.

Can I change application properties at runtime spring boot?

To change properties in a file during runtime, we should place that file somewhere outside the jar. Then we tell Spring where it is with the command-line parameter –spring. config. location=file://{path to file}.

How do I refresh application context in spring boot?

For a Spring Boot Actuator application, some additional management endpoints are available. You can use: POST to /actuator/env to update the Environment and rebind @ConfigurationProperties and log levels. /actuator/refresh to re-load the boot strap context and refresh the @RefreshScope beans.

How do I refresh application properties in spring boot?

For Reloading properties, spring cloud has introduced @RefreshScope annotation which can be used for refreshing beans. Spring Actuator provides different endpoints for health, metrics. but spring cloud will add extra end point /refresh to reload all the properties.


1 Answers

It sounds like you're looking for @RefreshScope which is provided by Spring Cloud. From the Spring Cloud documentation:

A Spring @Bean that is marked as @RefreshScope will get special treatment when there is a configuration change. This addresses the problem of stateful beans that only get their configuration injected when they are initialized. For instance if a DataSource has open connections when the database URL is changed via the Environment, we probably want the holders of those connections to be able to complete what they are doing. Then the next time someone borrows a connection from the pool he gets one with the new URL.

like image 121
Andy Wilkinson Avatar answered Oct 05 '22 23:10

Andy Wilkinson