Sub topic 12
Profile groups and includes
This chapter assumes you know how to activate a profile, how profile-specific files and multi-document YAML work, and how @Profile selects beans. Here you will learn how one profile can switch on several others.
The problem groups solve
A real production setup rarely has one profile. You might keep database settings in proddb and messaging settings in prodmq. Without help, every deployment must list both: --spring.profiles.active=prod,proddb,prodmq. Someone will forget one, and the application starts with half its configuration.
A profile group gives a single name to a set of profiles. Activating the group name activates every member.
Declaring a group
Groups are defined with the spring.profiles.group property. The key under it is the group name, and the value is the list of member profiles.
spring:
profiles:
group:
prod:
- proddb
- prodmq
config:
import: "optional:file:./local-overrides.yaml"With this file, --spring.profiles.active=prod activates prod, proddb and prodmq. Files such as application-proddb.yaml and application-prodmq.yaml are then loaded, and beans marked @Profile("proddb") are registered.
The spring.config.import line is a separate feature. It imports an optional local file that can add or override values without being committed to version control. The next chapter covers it.
Rules to remember
- Groups are expanded when you activate the group name. Setting
spring.profiles.active=proddbdoes not activateprod. - A group can only be defined in a place that is read before profiles are resolved, such as
application.yamlorapplication.properties. A group declared in a profile-specific file or a profile-conditional document is ignored. - Members can themselves be group names, so groups can nest. Avoid deep nesting, because it makes the active set hard to predict.
- Order matters when files conflict. Later profiles override earlier ones, and members of a group are added after the group name itself.
Groups and includes
Older Spring Boot versions offered spring.profiles.include. It also adds profiles unconditionally, but it is not allowed inside profile-specific documents in current versions. Use groups instead. They state the relationship in one place, which is the base configuration, and they work with every activation method.
Checking the result
Start the application and read the startup log. Spring Boot prints a line such as:
The following 3 profiles are active: "prod", "proddb", "prodmq"You can also verify the result in code with Environment.
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
class ProfilePrinter {
@Bean
CommandLineRunner printProfiles(Environment env) {
return args -> System.out.println(
"Active: " + Arrays.toString(env.getActiveProfiles()));
}
}Common errors
- Group defined in the wrong file. If members do not activate, check that the
spring.profiles.groupproperty is in the base file and not under anon-profiledocument. - Activating a member instead of the group. Groups only expand from the group name.
- Typos in profile names. Spring does not warn about a profile with no matching file or bean. The profile is active, but nothing changes.
Exercise
Your application.yaml defines the group prod with members proddb and prodmq. You start the app with --spring.profiles.active=proddb. Which profiles are active, and why?
Show answer
Only proddb is active. A group expands only when its own name is activated, and proddb is a member, not the group name. To get all three, activate prod.