Profile specific custom property 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 already supports profile-specific configuration through files such as application-dev.yml, but some projects also need custom property files for specific modules or environments. The clean approach is to use Spring Boot's config data system so the right files load declaratively and predictably.
Start with Built-In Profile Files
Before adding custom imports, use the standard profile mechanism where possible.
Activate the profile at runtime:
This is the simplest and best-documented configuration path, so it should be your default starting point.
Import Extra Files with spring.config.import
When you need more files than the built-in naming convention provides, use spring.config.import.
You can also place imports inside a profile-specific file.
The optional: prefix prevents startup failure when a file is intentionally absent in some environments.
Scope Custom Files to Specific Profiles
If a custom file should be loaded only for one profile, express that declaratively in profile files or activation blocks instead of scattering Environment checks through Java code.
This keeps the configuration story visible in one place and makes precedence easier to reason about.
Bind Values to Typed Classes
Custom property files are easier to use safely when they are bound into typed configuration objects.
Then enable configuration property scanning:
Typed binding is safer than scattering raw string lookups across the codebase.
Use @PropertySource Only for Narrow Legacy Cases
@PropertySource can still load .properties files, but it is usually a poor fit for modern profile-layered YAML configuration.
For most current Spring Boot applications, config-data imports are easier to understand and handle precedence more cleanly.
Debug Precedence and Missing Values
If the loaded values are not what you expect, increase config logging to see which sources were considered.
In non-production environments, actuator endpoints such as env and configprops can also help explain which property source won.
Common Pitfalls
One common problem is mixing older bootstrap-era patterns with modern config-data imports and then misreading property precedence.
Another is using @PropertySource for YAML-based profile setups and expecting it to behave like the config-data system. It does not provide the same profile-aware layering model.
Teams also create too many overlapping files with the same keys and no clear ownership. If every file can override every property, debugging quickly becomes difficult.
Summary
- Use built-in profile files first, because they are the simplest Spring Boot mechanism.
- Use
spring.config.importwhen you need additional custom property files. - Keep profile activation declarative instead of checking profiles manually in code.
- Bind settings into typed configuration classes for safer access.
- Use debug logging and actuator tools when you need to inspect precedence and property sources.
Related reading
- Programmatical approach in Java for file comparison
- Programmatically determine which Java thread holds a lock
- Programmatically shut down Spring Boot application
- Programmatically shut down Spring Boot application
- Programming a distributed application written in C#, Ruby and Java using XML-RPC
- Project 'org.springframework.bootspring-boot-starter-parent2.4.0' not found
- Prometheus Endpoint Not Working Spring Boot 2.0.0.RC1 Spring Webflux enabled
- Proof why does java.lang.String.hashCode's implementation match its documentation?

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.