Skip to main content

Dynamic Configuration Loading in Spring Boot: Handling Multiple Variants with a Primary Configuration

Shop Christmas Products Now

In this post, we'll discuss how to dynamically load and manage configurations in a Spring Boot application based on various variants or profiles. This approach is especially useful in scenarios like A/B testing, where each variant may have distinct configuration requirements, but there's also a need for a primary or default configuration.

We’ll demonstrate the solution using a generalized example while outlining the key concepts.


Use Case

Imagine you have a Spring Boot application that needs to load different configurations for various feature variants dynamically, while also maintaining a default configuration as the fallback. The system should:

  1. Dynamically load configuration properties from multiple sources.
  2. Register variant-specific configurations as Spring beans.
  3. Ensure the default configuration is marked as primary for injection wherever no variant is specified.
  4. Provide a mechanism to retrieve a specific configuration based on the variant name.

The Architecture

  1. Configuration Loading: Load configurations from external YAML files.
  2. Dynamic Bean Registration: Register each configuration variant as a Spring bean at runtime.
  3. Dynamic Bean Lookup: Provide an interface to fetch a specific variant’s configuration.



Implementation

1. Configuration Properties Model


@ConfigurationProperties(prefix = "feature-config") @Data @NoArgsConstructor @AllArgsConstructor public class FeatureConfigProperties { private Map<String, String> settings; }


This model represents a generic configuration for a feature. Each variant may have different values for the settings map.




2. Loading Configurations from YAML




# default.yml feature-config: settings: key1: "default-value1" key2: "default-value2" # variant1.yml feature-config: settings: key1: "variant1-value1" key2: "variant1-value2"



Assume these YAML files are stored in directories categorized by environment (e.g., dev, prod) and variant.


3. YAML Resource Loader

This component loads YAML properties for each variant.

@Slf4j public class YamlResourceLoader { private static final String CONFIG_BASE_PATH = "classpath:/config"; public Map<String, Properties> loadAllVariantProperties(String environment) { Map<String, Properties> resultMap = new TreeMap<>(); String searchPath = CONFIG_BASE_PATH + "/" + environment + "/**/*.yml"; try { Resource[] resources = new PathMatchingResourcePatternResolver().getResources(searchPath); for (Resource resource : resources) { if (resource.exists() && resource.isReadable()) { String variant = extractVariantFromPath(resource.getURL().getPath(), environment); Properties properties = loadYamlProperties(resource); resultMap.put(variant, properties); } } } catch (IOException e) { throw new RuntimeException("Failed to load variant properties", e); } return resultMap; } private Properties loadYamlProperties(Resource resource) { YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); yamlFactory.setResources(resource); return yamlFactory.getObject(); } private String extractVariantFromPath(String path, String environment) { return path.substring(path.indexOf(environment + "/") + (environment + "/").length(), path.indexOf('/')); } }


4. Dynamic Bean Registration

This component dynamically registers beans for each variant.


@Slf4j public class DynamicConfigurationRegistrar { private static final String DEFAULT_VARIANT = "default"; private final ApplicationContext applicationContext; public DynamicConfigurationRegistrar(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public void registerConfigurationBeans(Map<String, Properties> configMap, Environment environment) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory(); for (Map.Entry<String, Properties> entry : configMap.entrySet()) { String variant = entry.getKey(); Properties properties = entry.getValue(); registerConfigurationBean(registry, properties, FeatureConfigProperties.class, variant); } } private void registerConfigurationBean(BeanDefinitionRegistry registry, Properties properties, Class<?> clazz, String variant) { Binder binder = new Binder(new MapConfigurationPropertySource(properties)); Object beanInstance = binder.bind("feature-config", clazz).orElseThrow(); GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClass(clazz); beanDefinition.setInstanceSupplier(() -> beanInstance); if (DEFAULT_VARIANT.equals(variant)) { beanDefinition.setPrimary(true); } String beanName = clazz.getSimpleName() + "_" + variant; registry.registerBeanDefinition(beanName, beanDefinition); log.info("Registered configuration bean: {}", beanName); } }


5. Dynamic Configuration Lookup

A service to fetch beans dynamically by variant name


@Service public class ConfigurationLookupService { private final ApplicationContext applicationContext; @Autowired public ConfigurationLookupService(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public <T> T getConfigurationBean(String variant, Class<T> configClass) { String beanName = configClass.getSimpleName() + "_" + variant; if (applicationContext.containsBean(beanName)) { return applicationContext.getBean(beanName, configClass); } else { throw new RuntimeException("Configuration bean for variant '" + variant + "' not found."); } } }




6. Putting It All Together

A BeanFactoryPostProcessor to integrate the loader and registrar:


@Slf4j @Configuration public class ConfigurationLoader { @Bean public YamlResourceLoader yamlResourceLoader() { return new YamlResourceLoader(); } @Bean public DynamicConfigurationRegistrar dynamicConfigurationRegistrar(ApplicationContext context) { return new DynamicConfigurationRegistrar(context); } @Bean public BeanFactoryPostProcessor configurationPostProcessor( YamlResourceLoader loader, DynamicConfigurationRegistrar registrar) { return beanFactory -> { String environment = "prod"; // You can fetch this dynamically Map<String, Properties> configMap = loader.loadAllVariantProperties(environment); StandardEnvironment env = beanFactory.getBean(StandardEnvironment.class); registrar.registerConfigurationBeans(configMap, env); log.info("Dynamic configurations loaded successfully."); }; } }



Usage

Fetch configuration for a specific variant dynamically:


@Autowired private ConfigurationLookupService lookupService; public void useConfig() { FeatureConfigProperties defaultConfig = lookupService.getConfigurationBean("default", FeatureConfigProperties.class); FeatureConfigProperties variant1Config = lookupService.getConfigurationBean("variant1", FeatureConfigProperties.class); log.info("Default config: {}", defaultConfig.getSettings()); log.info("Variant1 config: {}", variant1Config.getSettings()); }




Conclusion

This approach provides a clean, extensible way to manage configuration variants dynamically. Key benefits include:

  1. Scalability: Easily add new variants without code changes.
  2. Separation of Concerns: Configuration and business logic are decoupled.
  3. Primary Fallback: Ensures a default configuration is always available.

You can adapt this design to any multi-variant scenario, making it ideal for dynamic and modular application architectures.





Comments

Popular posts from this blog

Advanced Kafka Resilience: Dead-Letter Queues, Circuit Breakers, and Exactly-Once Delivery

Introduction In distributed systems, failures are inevitable—network partitions, broker crashes, or consumer lag can disrupt data flow. While retries help recover from transient issues, you need stronger guarantees for mission-critical systems. This guide covers three advanced Kafka resilience patterns: Dead-Letter Queues (DLQs) – Handle poison pills and unprocessable messages. Circuit Breakers – Prevent cascading failures when Kafka is unhealthy. Exactly-Once Delivery – Avoid duplicates in financial/transactional systems. Let's dive in! 1. Dead-Letter Queues (DLQs) in Kafka What is a DLQ? A dedicated Kafka topic where "failed" messages are sent after max retries (e.g., malformed payloads, unrecoverable errors). ...

Project Reactor Important Methods Cheat Sheet

🔹 1️⃣ subscribeOn – "Decides WHERE the Pipeline Starts" 📝 Definition: subscribeOn influences the thread where the data source (upstream) (e.g., data generation, API calls) runs . It affects the source and everything downstream (until a publishOn switches it). Flux<Integer> flux = Flux.range(1, 3) .doOnNext(i -> System.out.println("[Generating] " + i + " on " + Thread.currentThread().getName())) .subscribeOn(Schedulers.boundedElastic()) // Change starting thread .map(i -> { System.out.println("[Processing] " + i + " on " + Thread.currentThread().getName()); return i * 10; }); flux.blockLast(); Output: [Generating] 1 on boundedElastic-1 [Processing] 1 on boundedElastic-1 [Generating] 2 on boundedElastic-1 [Processing] 2 on boundedElastic-1 [Generating] 3 on boundedElastic-1 [Processing] 3 on boundedElastic-1 📢 Key Insight: ...

🔄 Kafka Producer Internals: send() Explained with Delivery Semantics and Transactions

Kafka Producer Internal Working Apache Kafka is known for its high-throughput, fault-tolerant message streaming system. At the heart of Kafka's data pipeline is the Producer —responsible for publishing data to Kafka topics. This blog dives deep into the internal workings of the Kafka Producer, especially what happens under the hood when send() is called. We'll also break down different delivery guarantees and transactional semantics with diagrams. 🧠 Table of Contents Kafka Producer Architecture Overview What Happens When send() is Called Delivery Semantics Kafka Transactions & Idempotence Error Handling and Retries Diagram: Kafka Producer Internals Conclusion 🏗️ Kafka Producer Architecture Overview Kafka Producer is composed of the following core components: Serializer : Converts key/value to bytes. Partitioner : Determines which partition a record should go to. Accumulator : Buffers the records in memory be...