Spring Boot how to use multiple yml files
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Using multiple YAML files in Spring Boot is the normal way to separate shared defaults from environment-specific settings and feature-specific configuration. The important part is understanding which mechanism you are using: profile-specific files, multi-document YAML, or explicit imports with the config-data system.
Start with application.yml and Profile Files
Spring Boot always loads application.yml as the baseline configuration. Then it overlays profile-specific files such as application-dev.yml or application-prod.yml when those profiles are active.
A common layout looks like this:
Base settings:
Development overrides:
Production overrides:
The rule is simple: keep the shared defaults in application.yml, then override only the differences in profile files.
Activate Profiles Explicitly
Profile-specific files do nothing until the profile is active. You can activate profiles from the command line, environment, or tests.
Command-line activation:
Environment variable activation:
Test activation:
Being explicit matters in CI and deployment manifests. If you rely on defaults accidentally, you can end up running production code with development settings.
Split by Concern with spring.config.import
When configuration grows, profile files are not always enough. Spring Boot also supports importing additional config files:
An imported file might hold database settings:
And another might hold cache settings:
This pattern is useful when different teams own different sections of configuration or when one massive YAML file has become difficult to review safely.
Multi-Document YAML Is Another Option
If you prefer a single physical file, YAML supports multiple documents separated by ---. Spring Boot can activate documents conditionally:
This is handy for small services where you want profile-specific settings without creating many separate files. It becomes harder to navigate once the configuration grows large, so use it selectively.
Bind Configuration to Typed Classes
Multiple YAML files are easier to manage when the properties bind into a typed class instead of being fetched ad hoc from the environment:
Then enable scanning:
Typed binding helps catch invalid values earlier and makes configuration usage easier to test.
Know What Overrides What
Property precedence matters whenever the same key appears in multiple places. Environment variables and command-line arguments usually override packaged YAML values. Profile-specific files also override the baseline file for matching keys.
For critical settings, log the effective values at startup:
That small diagnostic step saves time when you are unsure which file actually won.
Common Pitfalls
- Putting every environment value in one file and then duplicating almost the whole file for each profile.
- Forgetting to activate the intended profile and assuming
application-dev.ymlorapplication-prod.ymlis being used. - Mixing older configuration patterns with current config-data imports without understanding precedence.
- Storing real production secrets directly in repository YAML files instead of external secret sources.
- Reading many individual properties manually instead of binding them into typed configuration classes.
Summary
- Use
application.ymlfor shared defaults and profile files for environment-specific overrides. - Activate profiles explicitly in local runs, tests, and deployments.
- Use
spring.config.importwhen you want separate YAML files by concern. - Consider multi-document YAML for small cases, but keep readability in mind.
- Bind configuration into typed classes so multi-file setups stay maintainable.
Related reading
- Spring boot http response compression doesn't work for some User-Agents
- spring boot https PKCS12 DerInputStream.getLength lengthTag111, too big
- Spring Boot in Docker
- Spring Boot Inherit application.properties from dependency
- Spring Boot integration tests AutoConfigureMockMvc and context caching
- Spring Boot Is it possible to use external application.properties files in arbitrary directories with a fat jar?
- Spring Boot, Java Config - No mapping found for HTTP request with URI /... in DispatcherServlet with name ''dispatcherServlet''
- Spring Boot Java Config Set Session Timeout

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.