The 30 second answer
Spring Boot uses the active profiles to load profile-specific application configuration alongside the base configuration. Within the same location, profile-specific values override the base values. If several profiles are active and define the same setting, the later profile wins, although the overall location and property-source precedence still matters. I can also make individual configuration documents conditional with spring.config.activate.on-profile. If I haven't activated a profile, Spring uses the default profile.
1. Find the active profiles. Profiles come from spring.profiles.active, which can be set in a file, an environment variable SPRING_PROFILES_ACTIVE, a command-line argument, or SpringApplication.setAdditionalProfiles. If none is set, the profile default is active (changeable with spring.profiles.default).
2. Resolve profile files for each location. The resolver loops over profiles and locations:
private Set<StandardConfigDataReference> getProfileSpecificReferences(ConfigDataLocationResolverContext context,
ConfigDataLocation[] configDataLocations, Profiles profiles) {
Set<StandardConfigDataReference> references = new LinkedHashSet<>();
for (String profile : profiles) {
for (ConfigDataLocation configDataLocation : configDataLocations) {
String resourceLocation = getResourceLocation(context, configDataLocation);
references.addAll(getReferences(configDataLocation, resourceLocation, profile));
}
}
return references;
}StandardConfigDataLocationResolver.java L141-L151
3. Priority rules.
- Profile files override non-profile files from the same locations.
- A profile file outside the jar beats one inside the jar.
- With
spring.profiles.active=prod,eu, values inapplication-eu.ymloverrideapplication-prod.yml: last wins.
4. Multi-document files. One YAML file can hold several documents separated by ---:
server:
port: 8080
---
spring:
config:
activate:
on-profile: prod
server:
port: 80The second document only applies when prod is active. The same works in .properties files with #---.
5. Profile groups. You can define a group so one profile turns on others:
spring:
profiles:
group:
prod: "proddb,prodmq"Rules that catch people:
spring.profiles.activecannot be set inside a profile-specific file or inside a document activated byon-profile. Spring Boot rejects this, because it would make the profile list depend on itself.spring.profiles.includeadds profiles but also can only be used in non-profile-specific documents.@Profile("prod")on a bean is a different feature: it controls bean registration, not property loading.
Editor's note: keep profiles for environment differences (dev, test, prod). Put secrets in environment variables or a secret store, not in application-prod.yml in Git.
Alternate interview scenarios
"Production loads application-dev.yml after a deployment, or a YAML document activates unexpectedly. How do you trace the profile activation path?"
I start from the startup log, which lists the active profiles, then find who set them. spring.profiles.active can come from a file, an environment variable such as SPRING_PROFILES_ACTIVE baked into the image or deployment, a system property or a command-line argument, and the usual precedence applies. Profile groups can switch on extra profiles, so a group under prod may pull in a profile I did not expect. For a single document, spring.config.activate.on-profile takes a profile expression or a list of expressions where at least one must match, so a list such as dev, prod is an OR, not an AND. I use an expression such as prod & !eu when both conditions must hold, and I check the env endpoint to see which document each value came from.
"How do profiles and external configuration interact?"
Profiles select beans and profile-specific files; they do not freeze those values against later sources. Runtime environment variables, command-line arguments and files outside the jar still override packaged profile files, in the property-source order described in the externalized-configuration answer. I confirm the active profiles at startup before I debug a value that looks wrong for this profile.
Sources (4)
- Spring Boot reference: Externalized configuration (property source order)docs.spring.io
- Spring Boot reference: Profile-specific filesdocs.spring.io
- Spring Boot reference: Profiles and profile groupsdocs.spring.io
- Spring Boot reference: Activation propertiesdocs.spring.io