Load Custom Yml Files Through Propertysource In Springboot

Elena Vance
-
load custom yml files through propertysource in springboot

Last Updated: Spring Boot: How to Split application.yml into Multiple YAML Files Using @PropertySource As Spring Boot applications grow in complexity, the primary application.yml configuration file can become bloated, hard to maintain, and difficult to navigate. This is especially true for large projects with numerous modules, environment-specific settings, or diverse configuration concerns (e.g., database, logging, third-party APIs). Splitting the application.yml into smaller, focused YAML files improves organization, readability, and maintainability.

While Spring Boot natively supports multi-document YAML files (using --- separators) and profile-specific files (e.g., application-dev.yml ), there are scenarios where you may want to split configurations into logically separated YAML files (e.g., database-config.yml , app-config.yml ) and load them explicitly. This is where @PropertySource comes into playâthough it requires extra setup to work with YAML files. In this blog, weâll explore how to split application.yml into multiple YAML files using @PropertySource , overcome its limitations with YAML, and implement a clean, maintainable configuration strategy.

Table of Contents# - Why Split Configuration Files? - Understanding @PropertySource in Spring Boot - Limitations of @PropertySource with YAML - Step-by-Step Guide to Split YAML Files - Advanced Scenarios - Common Pitfalls and Solutions - Conclusion - References Why Split Configuration Files?# Splitting application.yml into multiple YAML files offers several benefits: - Modularity: Separate concerns (e.g., database config in database.yml , logging inlogging.yml ). - Maintainability: Smaller files are easier to edit, review, and debug.

Reusability: Share config files across modules or projects (e.g., a common kafka-config.yml for microservices). - Environment Flexibility: Load different subsets of configurations based on profiles (e.g., database-dev.yml ,database-prod.yml ). Understanding @PropertySource in Spring Boot# @PropertySource is a core Spring annotation that allows you to register additional property sources (e.g., .properties or .yml files) with the Spring Environment . By default, it loads properties from .properties files, making them available for injection via @Value or @ConfigurationProperties .

Basic Example with .properties Files# To load a .properties file (e.g., app-config.properties ), youâd use @PropertySource on a @Configuration class: If app-config.properties contains app.name=MyApp , you can inject it with: Limitations of @PropertySource with YAML# While @PropertySource works seamlessly with .properties files, it has a critical limitation with YAML: The default PropertySourceFactory does not support YAML files. YAML files use a hierarchical structure (indentation-based) and support lists/objects, which require specialized parsing.

The default factory expects flat key-value pairs (like .properties ), so attempting to load a YAML file directly with @PropertySource("classpath:config.yml") will fail. To resolve this, we need a custom PropertySourceFactory that can parse YAML files and expose their properties as Spring PropertySource objects. Step-by-Step Guide to Split YAML Files# Letâs walk through splitting application.yml into smaller YAML files and loading them with @PropertySource . 1. Project Setup# Create a basic Spring Boot project (using Spring Initializr) with the following dependencies: - Spring Web: For REST endpoints (to validate configurations).

Configuration Processor: For @ConfigurationProperties support (optional but recommended for IDE autocompletion). 2. Create Additional YAML Files# Split your configuration into focused YAML files. For this example, weâll create three files: database-config.yml (Database-related settings)# app-config.yml (Application metadata)# logging-config.yml (Logging settings)# 3. Create a Custom PropertySourceFactory for YAML# To load YAML files with @PropertySource , implement a custom PropertySourceFactory using Springâs YamlPropertySourceLoader (a built-in utility for parsing YAML). Step 3.1: Define the Custom Factory# Create YamlPropertySourceFactory.java : 4.

Use @PropertySource with Custom Factory# Now, use @PropertySource on a @Configuration class, specifying the custom YamlPropertySourceFactory to load the YAML files. Step 4.1: Create a Configuration Class# Create SplitConfig.java : 5. Inject and Validate Properties# With the YAML files loaded, inject properties using either @Value (for individual properties) or @ConfigurationProperties (for grouped properties).

Option 1: Inject with @Value# Use @Value to inject individual properties into a bean (e.g., a REST controller): Option 2: Group Properties with @ConfigurationProperties# For hierarchical properties (e.g., database or app ), use @ConfigurationProperties to bind them to a POJO: Step 5.1: Create a DatabaseProperties POJO# Step 5.2: Inject DatabaseProperties into a Service# 6. Verify the Setup# Run the Spring Boot application and test the endpoints: - Access http://localhost:8080/config to see injectedapp.name anddatabase.url . - Logs should reflect the logging.level settings fromlogging-config.yml .

Advanced Scenarios# Environment-Specific Splits with Profiles# To load environment-specific YAML files (e.g., database-dev.yml for dev profile), combine @Profile with @PropertySource : Step 1: Create Profile-Specific YAML Files# database-dev.yml :database-prod.yml : Step 2: Use @Profile with @PropertySource# Ordering of Property Sources# If multiple YAML files define the same property, the last loaded property source takes precedence. For example: If both files define app.name , the value from app-overrides.yml will be used.

Common Pitfalls and Solutions# Conclusion# Splitting application.yml into multiple YAML files using @PropertySource is a powerful way to organize configurations in Spring Boot. By creating a custom PropertySourceFactory , you can overcome @PropertySource âs limitations with YAML and enjoy modular, maintainable, and environment-friendly configuration management. This approach is especially valuable for large applications, where keeping configurations organized reduces cognitive load and speeds up development.

People Also Asked

PropertySourcewith YAMLFilesin Spring Boot | Baeldung?

Last Updated: Spring Boot: How to Split application.yml into Multiple YAML Files Using @PropertySource As Spring Boot applications grow in complexity, the primary application.yml configuration file can become bloated, hard to maintain, and difficult to navigate. This is especially true for large projects with numerous modules, environment-specific settings, or diverse configuration concerns (e.g.,...

Spring Boot: How to Split application.ymlinto Multiple YAMLFiles...?

While Spring Boot natively supports multi-document YAML files (using --- separators) and profile-specific files (e.g., application-dev.yml ), there are scenarios where you may want to split configurations into logically separated YAML files (e.g., database-config.yml , app-config.yml ) and load them explicitly. This is where @PropertySource comes into playâthough it requires extra setup to work wi...

Spring @PropertySourceusing YAML - Stack Overflow?

Table of Contents# - Why Split Configuration Files? - Understanding @PropertySource in Spring Boot - Limitations of @PropertySource with YAML - Step-by-Step Guide to Split YAML Files - Advanced Scenarios - Common Pitfalls and Solutions - Conclusion - References Why Split Configuration Files?# Splitting application.yml into multiple YAML files offers several benefits: - Modularity: Separate concern...

Loadcustomymlfilesthrough@PropertySourceinspringboot?

Use @PropertySource with Custom Factory# Now, use @PropertySource on a @Configuration class, specifying the custom YamlPropertySourceFactory to load the YAML files. Step 4.1: Create a Configuration Class# Create SplitConfig.java : 5. Inject and Validate Properties# With the YAML files loaded, inject properties using either @Value (for individual properties) or @ConfigurationProperties (for grouped...

Read propertyfilesin using @PropertySourceinSpring | Jstobigdata?

Last Updated: Spring Boot: How to Split application.yml into Multiple YAML Files Using @PropertySource As Spring Boot applications grow in complexity, the primary application.yml configuration file can become bloated, hard to maintain, and difficult to navigate. This is especially true for large projects with numerous modules, environment-specific settings, or diverse configuration concerns (e.g.,...