Changing Spring Boot Properties at Runtime
Last Updated :
17 Sep, 2024
Spring Boot provides a flexible way to configure application properties, typically defined statically in the application.properties
or application.yml
files. However, there are scenarios where properties need to be changed dynamically at runtime without restarting the application. In this article, we will explore how to change Spring Boot properties at runtime and the different methods to achieve this.
Changing Properties in Spring Boot at Runtime
Changing properties at runtime involves updating the application's configuration without requiring a restart. This can be particularly useful for adjusting settings dynamically based on user input, environmental changes, or operational requirements.
Spring Boot applications are typically configured using properties files (application.properties
or application.yml
). These files are read at startup, and changes generally require a restart to take effect. However, it is sometimes necessary to update configuration settings while the application is running.
Key Components
- Environment Abstraction: Provides access to the application's configuration properties and allows for programmatic access and manipulation of the properties.
- PropertySources: Mechanism for adding, removing, or modifying property sources dynamically. This includes
PropertiesPropertySource
, which is a common way to create new property sources from Properties
objects. - Spring Cloud Config: For more complex scenarios involving multiple services or dynamic configuration across distributed systems, Spring Cloud Config provides a centralized configuration server.
Project Implementation to Change Properties at Runtime in Spring Boot
This example demonstrates how to change Spring Boot properties at runtime. We will create a simple Spring Boot application with a REST API that allows updating and retrieving properties dynamically.
Step 1: Create the New Spring Boot Project
Create a new Spring Boot project using IntelliJ IDEA with the following options:
- Name: dynamic-property-management
- Language: Java
- Type: Maven
- Packaging: Jar
Click Next and proceed with the default settings.
Step 2: Add Dependencies
Add the following dependencies into the Spring Boot project.
- Spring Web
- Lombok
- Spring DevTools
- Spring Boot Actuator
Click on the Create button.
Project Structure
After creation, the project structure should look like this:
Step 3: Configure Application Properties
Open the application.properties
file and add the following configuration:
spring.application.name=dynamic-property-management
app.mode=production
Step 4: Create the PropertyService
Class
Create the PropertyService
class to handle property operations.
PropertyService.java:
Java
package com.gfg.dynamicpropertymanagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.stereotype.Service;
import java.util.Properties;
@Service
public class PropertyService {
@Autowired
private ConfigurableEnvironment environment;
/**
* Updates a property in the environment.
*
* @param key The property key to update.
* @param value The new value for the property.
*/
public void updateProperty(String key, String value) {
MutablePropertySources propertySources = environment.getPropertySources();
Properties props = new Properties();
props.put(key, value);
PropertiesPropertySource propertySource = new PropertiesPropertySource("dynamicProps", props);
propertySources.addFirst(propertySource);
}
/**
* Retrieves a property value from the environment.
*
* @param key The property key to retrieve.
* @return The current value of the property.
*/
public String getProperty(String key) {
return environment.getProperty(key);
}
}
- updateProperty(): Adds or updates a property at runtime by creating a new
PropertiesPropertySource
and adding it to the environment's property sources. - getProperty(): Retrieves the current value of a property from the environment.
Step 5: Create the ConfigController
Class
Create the ConfigController
class to provide REST endpoints for updating and retrieving properties.
ConfigController.java:
Java
package com.gfg.dynamicpropertymanagement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.web.bind.annotation.*;
import java.util.Properties;
@RestController
@RequestMapping("/api/config")
public class ConfigController {
@Autowired
private ConfigurableEnvironment environment;
/**
* Endpoint to update a property at runtime.
*
* @param key The property key to update.
* @param value The new value for the property.
* @return Confirmation message.
*/
@PostMapping("/update")
public String updateProperty(@RequestParam String key, @RequestParam String value) {
MutablePropertySources propertySources = environment.getPropertySources();
Properties props = new Properties();
props.put(key, value);
PropertiesPropertySource propertySource = new PropertiesPropertySource("dynamicProps", props);
propertySources.addFirst(propertySource);
return "Property updated: " + key + " = " + value;
}
/**
* Endpoint to retrieve the current value of a property.
*
* @param key The property key to retrieve.
* @return The current value of the property.
*/
@GetMapping("/get")
public String getProperty(@RequestParam String key) {
return key + " = " + environment.getProperty(key);
}
}
Explanation:
- updateProperty(): Handles POST requests to update a property dynamically.
- getProperty(): Handles GET requests to retrieve the current value of a property.
Step 6: Main class
No changes are required in the main class.
Java
package com.gfg.dynamicpropertymanagement;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DynamicPropertyManagementApplication {
public static void main(String[] args) {
SpringApplication.run(DynamicPropertyManagementApplication.class, args);
}
}
pom.xml File:
XML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gfg</groupId>
<artifactId>dynamic-property-management</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dynamic-property-management</name>
<description>dynamic-property-management</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 7: Run the Application
Now, start the application. It will run on port 8080.
Step 8: Testing the Application
Open the postman tool to send the below request to update property at the runtime.
Update Property:
POST http://localhost:8080/api/config/update?key=app.mode&value=development
Response:
Retrieve Property:
GET http://localhost:8080/api/config/get?key=app.mode
Response:
This example demonstrates how to dynamically update and retrieve the properties in the Spring Boot application using the Environment abstraction.
Similar Reads
Spring Boot - @ConfigurationProperties
In Spring Boot, @ConfigurationProperties Annotation allows the developer to map the entire content of the properties file to a POJO (Plain Old Java Object). A property file can be either application.properties or application.yml. This annotation is useful when we have a large set of explicit configu
6 min read
Properties with Spring and Spring Boot
Java-based applications using the Spring framework and its evolution into the Spring Boot and the properties play a crucial role in configuring the various aspects of the application. Properties can allow the developers to externalize the configuration settings from the code. Understanding how to wo
4 min read
Spring Boot â Managing Application Properties with Profiles
In Spring Boot, managing application properties is crucial for configuring the application based on different environments (e.g., development, testing, production). Spring Boot provides a feature called Profiles to help with this. Profiles allow you to define different sets of properties for various
6 min read
Spring Boot - Create a Custom Auto-Configuration
Usually, we might be taking a maven project or grade project for Spring Boot related projects. We will be adding the dependencies in pom.xml (in case of a maven project). In a spring application, Spring Boot auto-configuration helps to automatically configure by checking with the jar dependencies th
4 min read
Spring Boot - Application Properties
Spring Boot is built on top of the Spring Framework and includes all its features while simplifying configuration and setup. It has become a favorite among developers because it provides a rapid, production-ready environment that allows them to focus on business logic instead of dealing with complex
3 min read
Spring Boot - Auto-configuration
Spring Boot is heavily attracting developers toward it because of three main features as follows: Auto-configuration - such as checking for the dependencies, the presence of certain classes in the classpath, the existence of a bean, or the activation of some property.An opinionated approach to confi
5 min read
Spring Boot - Externalized Configuration
In Spring Boot, externalized configuration is a feature that allows you to separate configuration settings from your code. This means you can change how your application behaves by modifying configuration files without needing to recompile or redeploy the application. Spring Boot supports various co
4 min read
Spring Boot - Configuring a Main Class
Spring Boot simplifies the process of creating and running Java applications by providing a set of conventions and auto configurations. The main class in the Spring Boot application acts as the entry point where the application starts the execution. It is responsible for bootstrapping the Spring con
4 min read
Spring Boot - Random/Dynamic Port Allocation
Unlike the Spring MVC project where we have to manually add and install the Tomcat server, Spring Boot comes with an embedded Tomcat server, so that the applications can be hosted on it. And this Tomcat server runs on a port number and the default port number is 8080. And in the Spring Boot applicat
7 min read
Spring Boot Security Auto-Configuration
Spring Boot Security Auto Configuration can simplify the process of securing the Spring Boot applications by providing default security configurations. It can automate the many common security tasks such as setting up the authentication, and authorization and it can handle the common security vulner
4 min read