Implementing a Custom Configuration Source
This guide shows how to create a custom ConfigurableValueSource to load dynamic variable values from sources other than HOCON files or DPS.
Overview
Custom configuration sources allow you to load dynamic variable values from:
-
Databases
-
Remote configuration services (Consul, etcd, AWS Parameter Store)
-
Environment variables
-
Custom file formats
-
Any other external source
Prerequisites
-
The
ipf-studio-configuration-coredependency -
Spring Boot application
-
Understanding of the
ConfigurableValueSourceinterface
Steps
1. Add Core Dependency
<dependency>
<groupId>com.iconsolutions.dsl.studio</groupId>
<artifactId>ipf-studio-configuration-core</artifactId>
<version>${ipf-studio-configuration.version}</version>
</dependency>
2. Implement ConfigurableValueSource
Create a class that implements the ConfigurableValueSource interface:
import com.iconsolutions.dsl.studio.configurablevalues.ConfigurableValue;
import com.iconsolutions.dsl.studio.configurablevalues.ConfigurableValueSource;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Component
public class DatabaseConfigurableValueSource implements ConfigurableValueSource {
private final DynamicConfigRepository repository;
public DatabaseConfigurableValueSource(DynamicConfigRepository repository) {
this.repository = repository;
}
@Override
public String getName() {
return "database";
}
@Override
public Integer getPrecedence() {
return 15; // Higher than HOCON (5) and default dynamic settings (10)
}
@Override
public List<ConfigurableValue> getValues() {
return repository.findAll().stream()
.map(entity -> new ConfigurableValue(
entity.getKey(),
entity.getValue()
))
.collect(Collectors.toList());
}
}
3. Understand Precedence
The precedence determines the order in which sources are processed. This is only applied at startup, once the application is running updates to the DynamicExpressionRegistry will be applied without considering the precedence levels. Higher values override lower values:
| Source | Precedence | Effect |
|---|---|---|
HOCON files |
5 |
Processed first (base values) |
Dynamic Settings |
10 |
Can override HOCON |
Your Custom Source |
15 |
Overrides both above |
Choose your precedence based on where your source fits in the override hierarchy.