Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

In a single application.yml, how do you set a property only when the 'prod' profile is active?

We split application.yml into several documents with a line containing three dashes, ---, and in the second document set spring.config.activate.on-profile: prod. Spring Boot reads the first document always and applies the second only when the prod profile is active, so its properties override the defaults just for prod. The older spring.profiles key inside a document is no longer supported for this in recent Spring Boot versions and must be replaced with spring.config.activate.on-profile.

Why this question is asked

Small and medium services often keep all their configuration in one application.yml rather than one file per profile, because it is easier to see every environment's differences side by side. The syntax for that changed in Spring Boot 2.4, and copying an older example is a common reason why a production value is silently ignored or startup fails. Interviewers ask this as a quick coding question to see whether we know the current syntax and how documents are ordered. In this answer, we will recap multi-document YAML, write the prod-only property, see how the documents are applied, look at the old syntax and what replaced it, and cover a few rules worth knowing.

1A Quick Recap of Multi-Document YAML

YAML allows several documents in one file, separated by a line containing exactly three dashes. Each document is a complete YAML tree of its own. Spring Boot reads a multi-document application.yml as if it were several files, one after another in the order they appear, and each document can carry an activation condition that decides whether it is used.

The same works in application.properties since Spring Boot 2.4: a line containing #--- or !--- separates documents there.

2Writing the Prod-Only Property

The first document holds the defaults that apply everywhere. The second document starts with the activation condition and then holds the properties for prod.

YAML
# application.yml
app:
cache-size: 100
payments:
mode: sandbox
---
spring:
config:
activate:
on-profile: prod
app:
payments:
mode: live
$ java -jar shop.jar
// app.payments.mode = sandbox, app.cache-size = 100
$ java -jar shop.jar --spring.profiles.active=prod
// app.payments.mode = live, app.cache-size = 100

Notice that cache-size is still 100 with prod active. The second document only overrides the keys it contains; everything else comes from the first document. That is why the prod document can stay short: it lists only what is different.

3How the Documents Are Applied

Spring Boot processes the documents in order. A document without an activation condition always applies. A document with spring.config.activate.on-profile applies only if one of the listed profiles, or the profile expression, matches the active profiles. When several applying documents set the same key, the later document wins, so profile documents should come after the defaults.

Step throughReading a multi-document application.yml
  1. Read document 1no condition, always applied
  2. Read document 2spring.config.activate.on-profile: prod
  3. Is prod active?
    YesApply document 2 over document 1NoSkip document 2
  4. Resolve each keylater applied documents win

4 steps in order. Play them, or step through them one at a time.

on-profile also accepts lists and expressions, such as on-profile: "prod & eu" or a YAML list of several names. And there is a second condition, spring.config.activate.on-cloud-platform, which applies a document only on a detected platform such as kubernetes, useful for settings that only make sense inside a cluster.

4The Old spring.profiles Key and What Replaced It

Before Spring Boot 2.4, the activation condition was written as spring.profiles inside the document. Many old answers and blog posts still show it.

Java
# old style, from Spring Boot 2.3 and earlier
---
spring:
profiles: prod # no longer supported for this; startup fails in recent versions
app:
payments:
mode: live
# current style
---
spring:
config:
activate:
on-profile: prod
app:
payments:
mode: live

Spring Boot 2.4 introduced the new config data processing and spring.config.activate.on-profile, and supported the old key only through a legacy mode. In Spring Boot 3 the legacy mode is gone, and using spring.profiles inside a document makes startup fail with an error saying the property is invalid and should be replaced with spring.config.activate.on-profile; check your version for the exact message. The reason for the change was that spring.profiles looked like it activated profiles, when it actually meant 'apply this document when these profiles are active', and the new name says what it does.

5Rules Worth Knowing

A profile-specific document cannot activate profiles. Setting spring.profiles.active or spring.profiles.include inside a document with on-profile fails, because Spring would have to know the active profiles to decide whether to read the document that changes them. Profile groups defined with spring.profiles.group belong in a document without conditions.

Separate files such as application-prod.yml still work and override documents in application.yml for the same profile, following the normal rule that profile-specific files override plain ones. Choosing between one file and several is a matter of taste: one file shows all differences together, several files are easier when each environment's configuration is owned by different people. Mixing both styles for the same profile makes it hard to know which value wins, so a team should pick one.

6Follow-up questions

Do multi-document files work with @PropertySource?

No. @PropertySource loads .properties files and does not support YAML or multi-document syntax. Multi-document files work for files loaded by Spring Boot's config data processing: application files and locations brought in with spring.config.import.

How do I make a document apply when a profile is not active?

Use a profile expression: on-profile: "!prod" applies the document in every case except when prod is active. This is handy for development conveniences, but it is usually clearer to put those values in the default document and override them for prod.

Can I see which document supplied a value?

Yes. The actuator env endpoint shows each property source; documents from one file appear as separate sources whose names include the file and the document number, so the winning source tells us which document applied.

7Conclusion

In this answer, we set a property for the prod profile only, inside a single application.yml. We separated documents with ---, put spring.config.activate.on-profile: prod at the top of the second document, and saw that it overrides only its own keys and only when prod is active, with later documents winning. The old spring.profiles key inside documents was replaced in Spring Boot 2.4 and no longer works in recent versions, and a profile document cannot itself activate profiles.

What to say in the interviewAbout 70 seconds aloud

Open with

I would make application.yml a multi-document YAML file. The first document holds the defaults for every environment. Then a line with three dashes starts a second document, and at the top of it I set spring.config.activate.on-profile: prod, followed by only the properties that differ in production, for example app.payments.mode: live.

Then, if they want more

Spring Boot reads the documents in order: the first always applies, the second only when prod is active, and when both set the same key the later document wins, so production gets live while every other key still comes from the defaults. on-profile also accepts expressions like prod and eu, or not prod.

The thing to watch is the old syntax: before Spring Boot 2.4 you wrote spring.profiles: prod inside the document. That was replaced by spring.config.activate.on-profile because the old name looked like it activated profiles, and in Spring Boot 3 using it fails startup with an error pointing to the new key.

Also, a profile-specific document cannot set spring.profiles.active or include, since Spring would need to know the profiles before reading it. The same multi-document idea works in properties files with a #--- separator.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 2.4, Spring Boot 2.3, Spring Boot 3
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.
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 "In a single application.yml, how do you set a property only when the 'prod' profile is...": https://scrivio-interview-library.netlify.app/spring-boot/spring-configuration/de3df03745/