Quick revision
The short answer
I would first list what each of the eight profiles changes, which usually shows that most differences are property values rather than beans. Those values move to external configuration, and profiles stay only for real behaviour differences, renamed after the behaviour, such as fake-payments, instead of after environments. Profile groups then compose a few building blocks per environment, and a startup log or check lists the active profiles and key settings so everyone can see what is running.
What to remember
In this answer, we simplified eight overlapping profiles. An inventory showed that most profiles only changed values, which moved to external configuration, while real behaviour differences stayed as profiles renamed after features such as fake-payments. Conditions written as x and !x cover every environment, profile groups give each environment a short recipe, production activates no special profile, and a startup report plus a build check keep the result visible. We migrated environment by environment, comparing beans and values before and after.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would first list what each of the eight profiles changes, which usually shows that most differences are property values rather than beans. Those values move to external configuration, and profiles stay only for real behaviour differences, renamed after the behaviour, such as fake-payments, instead of after environments. Profile groups then compose a few building blocks per environment, and a startup log or check lists the active profiles and key settings so everyone can see what is running.
Why this question is asked
Profiles start simple: one for local work and one for production. Years later there are eight, they overlap, and @Profile annotations with expressions like "prod & !prod-eu" decide which beans exist. Nobody can say which email sender runs in qa, and a bug that only appears in prod-us cannot be reproduced anywhere else. Interviewers ask this scenario to see whether we can clean up something tangled without breaking it. In this answer, we will recap why profile sprawl happens, take an inventory, separate values from behaviour, rename profiles after features, compose them with groups, make the result visible, and plan the migration.
1Why Profiles Sprawl
A profile is a named switch that controls which beans Spring registers, through @Profile, and which files Spring Boot loads, through application-{profile}.yml. Because one switch can change both values and code, it becomes the easy answer to every difference between environments. A new region needs a different URL, so someone adds prod-eu. A tester needs a different database, so someone adds qa. Each step is reasonable; together they produce a matrix nobody can reason about, and production runs code paths that no other environment exercises.
2Taking an Inventory
Before changing anything, we write down what each profile actually does. Searching for @Profile and listing every application-{profile} file gives the raw material. For each profile we record two lists: the beans it adds or removes, and the property values it sets. Running the application in each profile with the condition report, debug=true, and dumping the bean names gives the real picture, including beans affected indirectly through conditions.
| Profile | Bean changes | Property changes |
|---|---|---|
| dev, test, qa, staging | None | URLs, pool sizes, log levels |
| prod | Real SMTP and payment beans | URLs, sizes, credentials |
| prod-eu, prod-us | None | Region URLs, data residency settings |
| local | Fake email, fake payments | Local URLs |
| local-docker | Fake email | Container hostnames |
3Separating Values From Behaviour
The inventory usually shows that most differences are values: URLs, timeouts, region names, sizes. Values do not need profiles. They belong in external configuration: environment variables, ConfigMaps or a config server, one set per environment, while the jar keeps safe defaults. Once the values move out, profiles like qa, staging and prod-us contain nothing and can be deleted.
What remains are real behaviour differences, where a different bean must exist: a fake email sender that logs instead of sending, or a fake payment provider for local work. These are legitimate uses of profiles, and keeping only them makes the remaining @Profile annotations meaningful.
4Naming Profiles After Features
An environment-named profile in code, such as @Profile("prod"), tells us where a bean runs but not why. It also forces every new environment to decide whether it is prod-like. A feature-named profile says what changes, and any environment can pick it.
Notice that the 'after' version cannot leave an environment without a gateway, because the two conditions are exact opposites. That is a general benefit: a pair of profile conditions written as x and !x always covers every case, while lists of environment names always miss one eventually. Where a single property switch is enough, @ConditionalOnProperty with a clear name such as payments.provider=fake is an equally good choice.
5Composing With Profile Groups
With a few feature profiles left, profile groups give each environment a short, readable recipe. A group is defined once in application.yml and activated by name.
Notice that production activates nothing special. That is the strongest guarantee we can give: the production code path is the default code path, so every environment that does not opt into a fake exercises exactly what production runs.
6Making the Result Visible and Migrating Safely
To keep this from sprawling again, the application should say what it runs with. A small startup component logs the active profiles and a short list of key settings, such as the payment gateway class and the database host, and a test in the pipeline fails if a new environment-named profile appears in @Profile annotations.
The migration itself goes environment by environment. Before the change, we record the bean list and the effective values in each environment; after it, we compare. Differences that were intended are documented; any other difference is a bug in the migration. Starting with the environments that matter least, such as qa, gives the team practice before touching production.
7Follow-up questions
Why are environment-named profiles in code considered a smell?
Because they tie behaviour to a place rather than a reason, so every new environment must be added to many annotations, and production ends up running beans that no other environment runs. Feature names make the reason explicit and let any environment choose the behaviour it needs.
Is it acceptable to keep application-prod.yml?
Some teams keep one profile per environment that does nothing except load that environment's values, and that is manageable if no @Profile annotation in code uses it. My preference is still external configuration per environment, because then the values live with the deployment, not inside the artifact.
How do you stop the profile count from growing again?
A short written rule, a review checklist item and an automated check: profiles in code must be feature names from an agreed list, and a test that scans for @Profile values outside that list fails the build.
8Conclusion
In this answer, we simplified eight overlapping profiles. An inventory showed that most profiles only changed values, which moved to external configuration, while real behaviour differences stayed as profiles renamed after features such as fake-payments. Conditions written as x and !x cover every environment, profile groups give each environment a short recipe, production activates no special profile, and a startup report plus a build check keep the result visible. We migrated environment by environment, comparing beans and values before and after.
Open with
I would start with an inventory: for each of the eight profiles, list the beans it adds or removes and the property values it sets, using the files, the Profile annotations and a debug condition report per profile.
Then, if they want more
In my experience most profiles turn out to change only values like URLs, pool sizes and regions, and only one or two real behaviours differ, typically fake email or fake payments for local work. The values move to external configuration per environment, with safe defaults in the jar, and profiles like qa, staging or prod-us simply disappear.
The remaining behaviour differences get feature names instead of environment names: fake-payments instead of local or dev. Writing the pair as fake-payments and not fake-payments guarantees every environment gets exactly one implementation, which environment lists never do.
Then profile groups compose these blocks, so local activates fake-payments and fake-email, while production activates nothing, which means production runs the default code path. To keep it visible, I add a startup log of active profiles and key choices, like which gateway bean is active, and a build check that rejects environment-named profiles in code.
The migration goes environment by environment, starting with qa, comparing bean lists and effective values before and after.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A codebase has profiles dev, test, qa, staging, prod, prod-eu, prod-us and local-docker...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/6ba6413e26/