Springboot - multiple property files per profile
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot already supports profile-specific files such as application-dev.yml and application-prod.yml, but larger applications often need more than one configuration file per environment. The modern way to do that is to keep one logical profile active and import additional config files from that profile's main configuration.
In current Spring Boot versions, spring.config.import is usually the right tool. It is clearer than trying to fake one environment with many profiles or scattering configuration through several unrelated mechanisms.
The Default Spring Boot Model
Out of the box, Spring Boot loads:
- '
application.propertiesorapplication.yml' - then profile-specific variants such as
application-prod.yml
That is enough until one profile grows into a very large file containing database settings, mail settings, feature flags, integration endpoints, and local overrides. At that point, splitting by concern becomes easier to maintain.
Use spring.config.import Inside the Profile File
A clean pattern is to keep application-prod.yml as the profile entry point and import focused config files from there.
When prod is active, Spring Boot loads the base config, then application-prod.yml, then the imported files.
You can activate the profile in the usual way:
This keeps the meaning of prod simple while still letting configuration live in several files.
Why Not Activate Several Profiles Instead
Some teams try to model one environment by activating several profiles such as prod, db, and mail. That can work, but it usually mixes two different concerns:
- environment selection
- configuration modularity
If the files all belong to production, one active profile plus imports is easier to reason about than three or four profiles that only exist to simulate file splitting.
Use multiple active profiles when they express real independent dimensions of behavior. Do not use them only as a workaround for file organization.
@PropertySource Is Usually Not the Best Primary Tool
You can load extra property files with @PropertySource, but it is better suited to narrower bean configuration cases than to central application configuration.
The main drawback is timing and visibility. Properties loaded this way may arrive too late for some bootstrap decisions, and the full configuration picture becomes harder to trace as the application grows.
For most profile-level file splitting, spring.config.import is the cleaner and more modern answer.
Watch Property Precedence
Once you import several files, precedence matters. If the same key appears in multiple sources, the later one wins.
That can be helpful for deliberate overrides, but it can also hide mistakes. A good discipline is:
- keep files focused by concern
- avoid defining the same property in several places
- document intentional override order
For example:
- '
db-prod.ymlfor datasource settings' - '
mail-prod.ymlfor mail transport settings' - '
prod-overrides.ymlfor deployment-specific values outside version control'
That structure reduces accidental overlap.
External Files Are Often Worth It
For sensitive or environment-specific overrides, importing an external file can be cleaner than keeping everything in the packaged artifact.
The optional: prefix is useful when the application should still start without that file in some environments.
Common Pitfalls
The biggest mistake is using many profiles just to simulate multiple files for one environment. Another is relying on @PropertySource for configuration that should participate in the main Spring Boot config loading process. Teams also get into trouble when several imported files define the same keys and nobody documents which one is supposed to win. Finally, mixing classpath files, external files, and secret injection without a clear precedence plan makes incidents harder to debug.
Summary
- Spring Boot supports multiple files per profile by importing them from the profile's main config file.
- '
spring.config.importis the preferred modern mechanism.' - Keep one logical environment profile active unless multiple profiles represent real separate concerns.
- Use
@PropertySourcesparingly for narrower cases, not as the main profile-loading strategy. - Organize imported files by concern and document override order.
Related reading
- Springboot - validate RequestBody
- springboot 2.3.0 while connecting to h2 database
- Springboot 2.5.1 service doesn't stop on System.exit0
- SpringBoot 401 UnAuthorized even with out security
- SpringBoot application stuck at springboot logo
- SpringBoot Async requests throwing 503 Service Unavailable
- SpringBoot doesn't handle org.hibernate.exception.ConstraintViolationException
- springboot How to exclude configuration class in dependency dependency

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.