SoFunction
Updated on 2025-05-12

How to load multiple YAML configuration files in SpringBoot

Loading multiple YAML configuration files in Spring Boot is a common requirement and is often used to separate configuration information into multiple files for easy management and maintenance. Spring Boot provides a flexible way to load multiple YAML configuration files.

Here are some methods and steps to load multiple YAML configuration files in your Spring Boot application:

Method 1: Use attributes

Spring Boot 2.4 and above introduce properties to make it more convenient to load multiple configuration files. You can use OR files to import other YAML files.

For example, suppose you have the following two YAML files:

You can configure it like this in:

spring:
  config:
    import:
      - classpath:
      - classpath:

Method 2: Use and

By configuring different profiles, you can load different configuration files in different environments. Suppose you have the following files:

You can define some common configurations in , and then define configurations for specific environments in and .

For example,:

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/devdb
    username: devuser
    password: devpassword

spring:
  datasource:
    url: jdbc:mysql://prod-db-server:3306/proddb
    username: produser
    password: prodpassword

You can then specify the active profile via command line parameters or environment variables, for example:

java -jar  --=dev

Alternatively, use include other profile files in one profile file:

# 
spring:
  profiles:
    include: dev,custom

Method 3: Specify the configuration file location in

You can also specify the location of the YAML file through the attribute in the file.

For example:

=classpath:/,classpath:/

Method 4: Use @ConfigurationProperties and @PropertySource

While this is not a way to directly load multiple YAML files, you can convert YAML files into properties files and use the @PropertySource annotation to load them. You can then use @ConfigurationProperties to bind these properties to a configuration class.

For example, create a file:

custom.property1=value1
custom.property2=value2

Then, use @PropertySource and @ConfigurationProperties in your configuration class:

import ;
import ;
import ;

@Configuration
@PropertySource("classpath:")
@ConfigurationProperties(prefix = "custom")
public class CustomProperties {
    private String property1;
    private String property2;

    // getters and setters
}

Summarize

Spring Boot provides multiple methods to load multiple YAML configuration files, and which method to choose depends on your specific needs and application scenarios. Whether using , profiles, or @PropertySource, it can help you manage and load configuration information effectively.

This is the article about how SpringBoot loads multiple YAML configuration files. For more related SpringBoot loads multiple YAML content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!