How to add multiple application.properties files in spring-boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot can load configuration from several property files, but the right mechanism depends on why you are splitting the config. Profile-specific files work best for environment overrides. Imported files work best for grouping related settings. External locations work best when operations teams need to change values without rebuilding the jar.
Use Profile-Specific Files for Environments
The most common setup is one shared file plus one file per environment:
- '
application.properties' - '
application-dev.properties' - '
application-prod.properties'
Put defaults in the base file and environment-specific overrides in the profile files.
Enable a profile at startup:
This pattern is predictable and built into Boot. It should be your first choice when the only difference is environment.
Import Additional Property Files by Concern
If the goal is modularity rather than environment separation, use spring.config.import. This lets you break configuration into smaller files such as database, messaging, or feature settings.
This keeps one file from becoming a thousand-line dump of unrelated keys.
If a file is optional, mark it that way:
That is useful when a deployment may or may not mount an extra file.
Load External Files Without Rebuilding
Some values should not live in the packaged artifact at all. For those cases, point Boot at an external directory or file.
This is a good fit for deployment-specific configuration that changes independently of code. It also helps keep operational settings out of the classpath when the application is promoted across environments.
Use @PropertySource Carefully
@PropertySource still exists, but it is usually better for custom bean configuration than for core Boot startup settings. If you need a configuration class to read an extra property file, it can be appropriate:
However, for main application configuration, Boot's standard config loading rules are usually clearer. Relying on @PropertySource for everything makes startup behavior harder to reason about.
Understand Precedence Before Splitting Files
Multiple files only stay maintainable if everyone knows which source wins. Typical high-level precedence looks like this:
- command-line arguments
- external config files
- profile-specific packaged files
- base packaged files
That means duplicate keys are not merged in a magical way. One value wins according to precedence. If you split config across several files without documenting this, debugging turns into guesswork.
A Practical Layout
For many applications, a clean structure looks like this:
application.propertiesfor stable defaultsapplication-dev.propertiesandapplication-prod.propertiesfor environment overrides- imported module files for large config groups
- external files or secret stores for deployment-only sensitive data
This gives you separation without making startup behavior obscure.
Common Pitfalls
- Using
@PropertySourcefor core Boot settings when standard Boot config loading would be clearer. - Splitting keys across many files without documenting precedence.
- Duplicating the same property in several places and then guessing which one is active.
- Bundling secrets into source-controlled classpath files.
- Mixing profiles, imports, and external locations without a clear reason for each.
Summary
- Spring Boot supports multiple property files through profiles, imports, and external locations.
- Use profile files for environment-specific overrides.
- Use
spring.config.importto group related settings into focused files. - Use external locations when config should change without rebuilding the app.
- Keep the precedence model clear so another engineer can predict startup behavior quickly.
Related reading
- How to add parameters to HttpURLConnection using POST using NameValuePair
- How to add reference to a method parameter in javadoc?
- How to additionally configure autocreated Spring Boot beans?
- How to allow a User only access their own data in Spring Boot / Spring Security?
- How to alter databasechangelog.filename for Spring Boot and Liquibase?
- How to analyze a java thread dump?
- How to append a newline to StringBuilder
- How to append text to an existing file in Java?

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.