SoFunction
Updated on 2025-04-05

How to implement multiple yml mutual reading problem in springboot configuration file

Springboot configuration file implements multiple yml readings

In Spring Boot, there are many ways to realize mutual reading and combining configuration files.

If you want to use multiple YAMLs in a Spring Boot app (.yml) Configuration files, and you hope that these configuration files can read each other or overwrite certain configurations with each other, you can use the following methods:

1. Use properties (Spring Boot 2.4 and above)

Starting from Spring Boot 2.4, it has been introducedProperties that allow you to import other configuration files.

For example, in yourIn, you can do this:

spring:
  config:
    import: classpath:

This will be imported under the same class pathdocument.

2. Annotate using @PropertySource or @PropertySources

Although@PropertySourceAnnotations do not support YAML format files, but you can use it to load.propertiesFile in format.

If your configuration can be converted to.propertiesFormat, this is a viable option.

@Configuration
@PropertySource("classpath:")
public class AppConfig {
    // ...
}

For multiple configuration files, you can use@PropertySourcesannotation.

3. Use

In your main configuration file, you can specify profiles for other configuration files you want to include.

For example,middle:

spring:
  profiles:
    include: 
      - additional

Then, you can have a name calledthe file, it will be loaded automatically.

4. Use different configuration file names

Spring Boot will read by defaultordocument.

You can load different configuration files by specifying different configuration file names at startup.

For example:

java -jar  --=another-application

This will loadInstead

5. Use profiles

You can define multiple profiles in a YAML file, each profile has a different configuration.

Then, you can set it at runtimeTo specify which profile is active.

spring:
  profiles: dev
---
spring:
  profiles: prod

Then use the command line parameters to activate the specific profile:

java -jar  --=prod

Use in combination

These methods can be combined as needed to implement more complex configuration management strategies.

For example, you can use it in the main configuration fileTo import other configuration files, and use profiles to define configuration variables in a specific environment.

For example, if you have a nameThe file, which contains configurations specially customized for the local development environment, you can set it upforlocalto activate this configuration file.

This can be achieved in a number of ways:

1. Set in or

In your main configuration file, you can set:

spring:
  profiles:
    active: local

Or, if you are using.propertiesFormat:

=local

This will be activated by defaultlocal profile。

2. Pass command line parameters

When starting the application, you can specify the active profile through command line parameters:

java -jar  --=local

3. Set in environment variables

You can also specify an active profile by setting environment variables.

This is especially useful in some deployment environments:

export SPRING_PROFILES_ACTIVE=local

Then launch your application.

Things to note

  • whenWhen set, Spring Boot merges(or) and the configuration file corresponding to the profile (such as). If there are any conflicting configurations, profile-specific configurations override settings in the main configuration file.
  • You can activate multiple profiles at the same time, just separate them with commas when setting, e.g.local,dev
  • When using profile, make sure your configuration file naming followsapplication-{profile}.ymlformat. For example,localProfile, the file should be named

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.