Skip to main content

Dynamically Loading YAML Files in Spring Boot with Java

In modern applications, configuration plays a crucial role in maintaining flexibility and modularity. Often, we need to load properties from multiple YAML files, organized across nested directories, to ensure scalability and modular configuration. In this blog, we’ll explore how to dynamically load all YAML files from a specific directory structure and merge their properties into a single configuration object.


Use Case

Imagine we have a configuration directory like this:

src/main/resources

├── application.yml

├── configs

│   ├── environment

│   │   ├── dev.yml

│   │   ├── qa.yml

│   ├── features

│   │   ├── feature1.yml

│   │   ├── feature2.yml


We want to load all YAML files under the configs directory (including subdirectories), merge them, and make the properties available for use in our Spring Boot application.


Implementation

1. Dynamic YAML Loader

We will create a utility class to dynamically load and merge all YAML files into a single Properties object.

package com.example.config;


import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;

import org.springframework.core.io.Resource;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;


import java.io.IOException;

import java.util.Properties;


public class DynamicYamlLoader {


    public static Properties loadYamlFiles(String basePath) throws IOException {

        Properties combinedProperties = new Properties();

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();


        // Use '**' to include all nested directories and `.yml` files

        Resource[] resources = resolver.getResources(basePath + "/**/*.yml");


        for (Resource resource : resources) {

            if (resource.exists() && resource.isReadable()) {

                YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();

                yamlFactory.setResources(resource);

                Properties yamlProperties = yamlFactory.getObject();

                if (yamlProperties != null) {

                    combinedProperties.putAll(yamlProperties);

                }

            }

        }


        return combinedProperties;

    }

}

2. Spring Configuration Loader

To integrate this loader into Spring, we’ll create a configuration class that loads the properties and makes them available in the application context.


package com.example.config;


import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


import java.io.IOException;

import java.util.Properties;


@Configuration

public class AppConfig {


    @Bean

    public Properties dynamicProperties() throws IOException {

        // Specify the base directory

        String basePath = "classpath:/configs";

        return DynamicYamlLoader.loadYamlFiles(basePath);

    }

}


3. Example YAML Files

Here are some sample YAML files to simulate a realistic use case:

configs/environment/dev.yml:


app:

  name: DemoApp

  environment: development

logging:

  level: DEBUG

configs/features/feature1.yml:

feature1:

  enabled: true

  maxRetries: 3

configs/features/feature2.yml:

feature2:

  enabled: false

  timeout: 5000

4. Accessing Properties

You can now access the combined properties in your application. For example:

package com.example.service;


import org.springframework.stereotype.Service;


import java.util.Properties;


@Service

public class PropertyService {


    private final Properties properties;


    public PropertyService(Properties properties) {

        this.properties = properties;

    }


    public void printProperties() {

        System.out.println("App Name: " + properties.getProperty("app.name"));

        System.out.println("Feature1 Enabled: " + properties.getProperty("feature1.enabled"));

        System.out.println("Feature2 Timeout: " + properties.getProperty("feature2.timeout"));

    }

}


When running the application, the output will be:

App Name: DemoApp

Feature1 Enabled: true

Feature2 Timeout: 5000


Explanation

  1. Recursive File Loading:

    • The PathMatchingResourcePatternResolver ensures all YAML files under the specified path (including subdirectories) are discovered.
  2. Merging Properties:

    • Using Properties.putAll() allows us to combine properties from all YAML files. If duplicate keys exist, the last-loaded file overwrites the earlier values.
  3. Spring Integration:

    • By declaring a @Bean, we make the combined properties available for dependency injection.

 


 


Comments

Popular posts from this blog

Learning How to Map One-to-Many Relationships in JPA Spring Boot with PostgreSQL

  Introduction In this blog post, we explore how to effectively map one-to-many relationships using Spring Boot and PostgreSQL. This relationship type is common in database design, where one entity (e.g., a post) can have multiple related entities (e.g., comments). We'll dive into the implementation details with code snippets and provide insights into best practices. Understanding One-to-Many Relationships A one-to-many relationship signifies that one entity instance can be associated with multiple instances of another entity. In our case: Post Entity : Represents a blog post with fields such as id , title , content , and a collection of comments . Comment Entity : Represents comments on posts, including fields like id , content , and a reference to the post it belongs to. Mapping with Spring Boot and PostgreSQL Let's examine how we define and manage this relationship in our Spring Boot application: Post Entity  @Entity @Getter @Setter @Builder @AllArgsConstructor @NoArgsCon...

Tree Based Common problems and patterns

  Find the height of the tree. public class BinaryTreeHeight { public static int heightOfBinaryTree (TreeNode root) { if (root == null ) { return - 1 ; // Height of an empty tree is -1 } int leftHeight = heightOfBinaryTree(root.left); int rightHeight = heightOfBinaryTree(root.right); // Height of the tree is the maximum of left and right subtree heights plus 1 for the root return Math.max(leftHeight, rightHeight) + 1 ; } Find the Level of the Node. private static int findLevel (TreeNode root, TreeNode node, int level) { if (root == null ) { return - 1 ; // Node not found, return -1 } if (root == node) { return level; // Node found, return current level } // Check left subtree int leftLevel = findLevel(root.left, node, level + 1 ); if (leftLevel != - 1 ) { return leftLevel; // Node found ...

Understanding the Advertisement Domain: A Comprehensive Overview Part 2

 The advertisement domain is a complex and dynamic ecosystem that involves various technologies and platforms working together to deliver ads to users in a targeted and efficient manner. The primary goal is to connect advertisers with their target audience, increasing brand visibility, user engagement, and revenue generation. In this blog, we will delve into the different components of the advertisement ecosystem, key concepts like programmatic advertising and real-time bidding (RTB), and provide a practical example to illustrate how it all works. Key Components of the Advertisement Domain The advertisement domain broadly consists of the following components: Advertisers : These are brands or companies that want to promote their products or services through advertisements. They set up ad campaigns targeting specific user segments. Publishers : These are websites, mobile apps, or digital platforms that display ads to users. Publishers monetize their content by selling ad space to ad...