Sub topic 12
Profile groups and includes
This chapter assumes you can already activate a profile and write profile-specific files (application-prod.yaml) and @Profile beans. It also assumes you know that Spring Boot's later property sources override earlier ones. If any of that is new, review the chapters on profiles and profile-specific files first.
The problem: one environment, many profiles
Real environments rarely map to a single profile. A production deployment might need production database settings, production messaging settings and production mail settings. You can keep each concern in its own file (application-proddb.yaml, application-prodmq.yaml). Then every deployment script must remember to pass --spring.profiles.active=prod,proddb,prodmq. A forgotten name means a half-configured application.
Spring Boot offers two ways to say "this profile implies those profiles":
- Profile groups (
spring.profiles.group.<name>) define a named set. Activating the group name activates every member. - Profile includes (
spring.profiles.include) add profiles unconditionally to whatever else is active.
Both are applied while Boot reads configuration, so every profile-aware feature sees the expanded set. Those features are profile-specific files, multi-document on-profile sections, @Profile beans and Environment.getActiveProfiles().
Defining a profile group
Define the group in a document that is not itself profile-specific, usually the main application.yaml:
spring:
profiles:
group:
prod:
- proddb
- prodmq
config:
import: "optional:file:./local-overrides.yaml"With this file, the following command activates prod, proddb and prodmq:
java -jar app.jar --spring.profiles.active=prodThe .properties equivalent uses a comma-separated list:
spring.profiles.group.prod=proddb,prodmqThe spring.config.import line in the example is unrelated to groups. It only shows that both settings can live in the same document.
What the members activate
Each member behaves like any other active profile:
application-proddb.yamlandapplication-prodmq.yamlare loaded.- Multi-document sections with
on-profile: proddbmatch. - Beans annotated
@Profile("proddb")are registered.
The group name is a real profile too. application-prod.yaml is still loaded and @Profile("prod") still matches.
Here is a small runner that prints what is active:
import java.util.Arrays;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
class ActiveProfilePrinter implements ApplicationRunner {
private final Environment environment;
ActiveProfilePrinter(Environment environment) {
this.environment = environment;
}
@Override
public void run(ApplicationArguments args) {
System.out.println("Active profiles: "
+ Arrays.toString(environment.getActiveProfiles()));
}
}Run it with --spring.profiles.active=prod and the group above. The output lists prod, proddb and prodmq.
Ordering and overrides
When several profiles are active, the last one wins for conflicting keys. Boot places group members after the group name. If application-prod.yaml and application-proddb.yaml both set app.db.pool-size, the value from proddb wins. Members also follow the order in the list, so prodmq overrides proddb on a conflict.
Keep members focused on separate concerns. If two members set the same key, the winner depends on list order. That is easy to forget and hard to spot.
Profile includes
spring.profiles.include adds profiles whenever the document containing it is loaded:
spring:
profiles:
include:
- observabilityNow observability is always active, whichever profile you pass on the command line. The ability to replace a file's setting is the difference that matters here. Suppose application.yaml sets spring.profiles.active: dev and you launch with --spring.profiles.active=prod. The command-line value replaces the file value, so dev is no longer active. An include is additive instead. observability stays active whatever the command line says.
Groups compared with includes
| Aspect | Group | Include |
|---|---|---|
| Property | `spring.profiles.group.<name>` | `spring.profiles.include` |
| Trigger | Only when `<name>` is active | Whenever the document is loaded |
| Typical use | "prod means these three profiles" | "this profile is always on" |
| Replaced by `spring.profiles.active` | No | No |
| Allowed in a profile-specific document | Define it in a non-profile document | No, Boot rejects it |
Choose a group when the extra profiles belong to a named environment. Choose an include for cross-cutting profiles that apply everywhere.
Groups with other activation methods
A group expands no matter how the profile was activated. These all trigger it:
--spring.profiles.active=prodon the command line.- The
SPRING_PROFILES_ACTIVE=prodenvironment variable. @ActiveProfiles("prod")on a test class.SpringApplication.setAdditionalProfiles("prod")in code.
The group definition itself can also come from outside the file, because it is an ordinary property:
SPRING_PROFILES_GROUP_PROD=proddb,prodmq SPRING_PROFILES_ACTIVE=prod java -jar app.jarProperty precedence still applies. A group defined by an environment variable replaces a group of the same name from application.yaml. It does not merge with it.
Using groups with `@Profile` beans
Because members are real active profiles, you can target them precisely:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
class InfrastructureConfig {
@Bean
@Profile("proddb")
String databaseDescription() {
return "production database";
}
@Bean
@Profile("prodmq")
Runnable queueListener() {
return () -> System.out.println("listening on production queue");
}
}Activating only prod registers both beans through the group. Activating dev registers neither. A test can activate just proddb with @ActiveProfiles("proddb") to exercise one slice without the other.
Common mistakes
- Defining the group in a profile-specific file. Put the definition in
application.yaml(or an unconditional document), not inapplication-prod.yaml. - Expecting groups to nest by magic. A member can itself be the name of another group, but long chains are hard to read. Prefer a flat list.
- Using
includefor environment-specific logic. It always applies. Anything that should only apply in prod belongs in a group. - Relying on member order for correctness. Overlapping keys between members are a maintenance trap. Give each member a distinct set of keys.
- Misspelling a member name. An unknown profile name is not an error. Boot activates it, finds no file, and moves on. Print
getActiveProfiles()or check thelogging.level.org.springframework.boot.context.config=DEBUGoutput to see what was loaded.
Exercise
Your application.yaml contains spring.profiles.group.prod: [proddb, prodmq] and spring.profiles.include: [observability]. You start the app with --spring.profiles.active=staging. Which profiles are active, and what would you add to the command line to also get proddb and prodmq?
Show answer
The active profiles are staging and observability. The group does not apply because prod is not active, while the include applies unconditionally. To get proddb and prodmq you would activate prod as well, for example --spring.profiles.active=staging,prod. That adds the group members proddb and prodmq, and observability stays active through the include. Note that prod and its files then also load.
Exercise
Why does Boot reject spring.profiles.include in a document marked spring.config.activate.on-profile: prod, and what should you use instead?
Show answer
Profile activation is decided before profile-specific documents are evaluated, so a document that only applies once a profile is active cannot change which profiles are active. Define a profile group in a non-profile-specific document instead, such as spring.profiles.group.prod: [proddb, prodmq].