Dynamically Load Spring Properties from External Repositories.

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...

Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Spring Boot tutorials, java and spring cloud tutorials where we can cover other parts like microservices, etc.
Introduction In Java interviews, questions about the Map interface and its implementations like HashMap, ConcurrentHashMap, SynchronizedMap, and TreeMap often test both theoretical knowledge and practical problem-solving skills. This guide provides a...
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
Managing configurations dynamically is a crucial requirement in modern applications. In this article, we'll explore how to dynamically load Spring properties from external repositories by extending the EnvironmentPostProcessor and EnumerablePropertySource classes. This approach allows you to integrate external configuration sources seamlessly into your Spring application, ensuring flexibility and scalability.
Dynamic property loading is beneficial for several scenarios:
Centralized Configuration: Applications using a central configuration repository (e.g., AWS Parameter Store, Consul, etc.) can dynamically fetch settings during startup or runtime.
Environment-Specific Settings: Load properties specific to a deployment environment without modifying application code.
Enhanced Security: Sensitive properties can be securely fetched from vaults or encrypted repositories.
EnvironmentPostProcessor and EnumerablePropertySourceEnvironmentPostProcessor:
Used to customize the Spring Environment before the application context is refreshed.
Ideal for adding or modifying property sources programmatically.
EnumerablePropertySource:
Represents a property source with enumerable property names.
Used to define custom property sources for external repositories.
Make sure your pom.xml or build.gradle includes the required dependencies to access external repositories (e.g., for HTTP, AWS, or Consul).
pom.xml):<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
EnvironmentPostProcessorThe EnvironmentPostProcessor is invoked early in the application startup lifecycle, enabling you to register custom property sources.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
public class CustomPropertyLoader implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
// Register the custom property source
CustomPropertySource propertySource = new CustomPropertySource("customProperties");
environment.getPropertySources().addLast(propertySource);
}
}
EnumerablePropertySourceThe EnumerablePropertySource is responsible for fetching properties dynamically from an external repository.
import org.springframework.core.env.EnumerablePropertySource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CustomPropertySource extends EnumerablePropertySource<Map<String, String>> {
private final Map<String, String> properties = new HashMap<>();
public CustomPropertySource(String name) {
super(name);
loadProperties();
}
private void loadProperties() {
// Fetch properties dynamically from an external source (e.g., REST API, database)
properties.put("app.dynamic.property1", "value1");
properties.put("app.dynamic.property2", "value2");
}
@Override
public String[] getPropertyNames() {
Set<String> keys = properties.keySet();
return keys.toArray(new String[0]);
}
@Override
public Object getProperty(String name) {
return properties.get(name);
}
}
EnvironmentPostProcessorTo register the EnvironmentPostProcessor, create a spring.factories file in the META-INF directory.
META-INF/spring.factories):org.springframework.boot.env.EnvironmentPostProcessor=com.example.CustomPropertyLoader
Once the custom property loader is registered, you can access the dynamically loaded properties like any other Spring property.
application.properties):app.dynamic.property1=${app.dynamic.property1:defaultValue1}
app.dynamic.property2=${app.dynamic.property2:defaultValue2}
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropertyReader {
@Value("${app.dynamic.property1}")
private String property1;
@Value("${app.dynamic.property2}")
private String property2;
public void printProperties() {
System.out.println("Dynamic Property 1: " + property1);
System.out.println("Dynamic Property 2: " + property2);
}
}
Here are some additional ideas to extend the implementation:
Fetch Properties from a REST API: Replace the loadProperties() method in CustomPropertySource to retrieve data from an external REST endpoint.
private void loadProperties() {
// Simulate a REST call
properties.put("api.key", "12345");
properties.put("api.secret", "secretValue");
}
Use a Vault for Secure Properties: Integrate with services like HashiCorp Vault to dynamically load secure configurations.
Refresh Properties at Runtime: Combine with Spring Boot Actuator to refresh dynamic properties without restarting the application.
Decoupled Configuration Management: No need to bundle all configurations with the application.
Enhanced Flexibility: Dynamically load configurations based on external conditions or user needs.
Improved Security: Store sensitive properties securely in external systems.
Spring Framework Documentation:
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-environment
Spring Boot External Configuration:
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config
Using HashiCorp Vault with Spring Boot:
https://spring.io/guides/gs/vault-config/
By following this approach, you can implement a flexible, secure, and scalable solution for dynamic property management in your Spring Boot application.
More such articles:
https://www.youtube.com/@maheshwarligade