Reload Spring Boot application.properties in runtime

Thameem Ansari
2 min readJan 4, 2022

--

In this article, we are going to explore how to reload application properties in spring boot.

We have many options available in spring boot, but we will pick the easiest one.

Refresh beans with @ConfigurationProperties

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.

Maven dependencies

Spring actuator

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Spring cloud starter

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>

Application properties

Then, add below property to application properties file

management.endpoints.web.exposure.include=refresh

// Values to be stored in a Map
endpoint.url.aws=localhost:8080/aws/
endpoint.url.azure=localhost:8080/azure/

Endpoint class

Create a class for configuring properties and add @RefreshScope annotation to class.

@Component
@ConfigurationProperties(prefix = "endpoint")
@RefreshScope
public class EndpointProperties {

// This is for storing application properties in a Map
public Map<String, String> url;

public Map<String, String> getUrlProps() {
return url;
}

public void setUrlProps(Map<String, String> url) {
this.url = url;
}

// Get property value using key
public String getUrlProps(String key) {
return url.get(key);
}
}

This code creates a scope for refreshing application properties data.

Now, when you change data in application properties. we need to give a POST REST call using below URL.

http://localhost:8080/actuator/refresh

After accessing the URL, we can get new value from the next access.

Happy coding..

--

--

Thameem Ansari

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com