Sub topic 9
Profiles: what they are and how to activate them
This chapter assumes you can build a Spring Boot application with Maven or Gradle, that you know what a Spring bean is, and that you can read YAML and .properties files. It covers what a profile is and every common way to turn one on. Profile-specific files, @Profile beans and profile groups each get their own chapter, so here they appear only briefly.
What a profile is
A profile is a named label that you attach to a running application. Spring Boot uses the active labels to decide two things:
- Which configuration files and sections are loaded.
- Which beans are registered in the application context.
The usual labels are dev, test and prod, but a profile name is any string you choose. A profile has no behavior of its own. It only matters when some configuration or bean says "apply me when this label is active".
Without profiles, you would have to edit files or rebuild the application to move it from your laptop to production. With profiles, you build one artifact and change the active label at startup.
The default profile
If you activate nothing, Spring uses a profile named default. You can see this in the startup log:
No active profile set, falling back to 1 default profile: "default"Once any profile is active, default is no longer active. This catches people out. A bean marked @Profile("default") disappears as soon as you start the application with prod.
Activating profiles
You can set spring.profiles.active in several places. It is an ordinary property, so the precedence rules from the earlier chapter apply to it. Higher-precedence sources win.
| Method | Example | Typical use |
|---|---|---|
| Command-line argument | `--spring.profiles.active=prod` | Starting a jar by hand |
| Environment variable | `SPRING_PROFILES_ACTIVE=prod` | Containers and servers |
| JVM system property | `-Dspring.profiles.active=prod` | Scripts and IDE run configurations |
| Configuration file | `spring.profiles.active: dev` | A local default |
| Test annotation | `@ActiveProfiles("prod")` | Tests |
The command line and environment variable forms look like this:
# Command-line argument
java -jar app.jar --spring.profiles.active=prod
# Environment variable (relaxed binding maps it to spring.profiles.active)
SPRING_PROFILES_ACTIVE=prod java -jar app.jar
# Several profiles, separated by commas
java -jar app.jar --spring.profiles.active=prod,metricsEnvironment variables are the usual choice in containers. You set the variable in the deployment descriptor and leave the image unchanged.
Activating several profiles
Separate names with commas. All listed profiles are active at the same time. If two of them define the same property, the one listed later wins.
Adding profiles instead of replacing them
spring.profiles.active replaces any value set in a lower-precedence source. If a file sets dev and the command line sets prod, only prod is active. To add a profile without replacing the others, use spring.profiles.include:
spring:
profiles:
include:
- metricsIncluded profiles are always active alongside whatever else is chosen. Use this sparingly. The group mechanism in a later chapter handles most of these cases more clearly.
A minimal example
The following file gives defaults for local development and changes the mail host and port only when prod is active. The --- line splits the file into two documents.
app:
mail:
host: localhost
port: 1025
sender: [email protected]
retry-attempts: 3
---
spring:
config:
activate:
on-profile: prod
app:
mail:
host: smtp.example.com
port: 587The first document has no condition, so it always applies. The second document applies only when prod is active. Properties it defines override the first document, and properties it does not define keep their earlier values. With prod active, sender and retry-attempts stay as they were. The details of multi-document files are in the profile files chapter.
You can also pick beans by profile. This code registers exactly one Notifier, depending on the active profile:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
interface Notifier {
void send(String message);
}
@Configuration
class NotifierConfig {
@Bean
@Profile("!prod")
Notifier loggingNotifier() {
return message -> System.out.println("[dev] " + message);
}
@Bean
@Profile("prod")
Notifier emailNotifier(MailProperties mail) {
return message -> System.out.println("Sending via " + mail.host() + ": " + message);
}
}The !prod expression means "any time prod is not active". MailProperties is the properties record bound in a later chapter. The point here is that one switch, the active profile, changes both the properties and the bean.
How the choice flows through the application
Activation happens early, before beans are created. Spring Boot reads the active profile names first, then loads the files and registers the beans that match.
Setting a profile for a single test run
In a test, use @ActiveProfiles. It applies only to that test class and does not touch your real configuration.
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
@SpringBootTest(properties = "app.mail.port=2525")
@ActiveProfiles("prod")
class MailPropertiesTest {
@Autowired
MailProperties mail;
@Test
void usesProdHostAndTestPort() {
assertThat(mail.host()).isEqualTo("smtp.example.com");
assertThat(mail.port()).isEqualTo(2525);
}
}Common errors and edge cases
- Typos in profile names. Spring does not warn about a name that nothing uses. If
--spring.profiles.active=porodmatches no file or bean, the application starts with defaults. Check the startup log lineThe following 1 profile is active. - Assuming
defaultstays active. As noted above, it turns off when you activate anything else. - Zero or two beans. If your
@Profileconditions leave no bean of a required type, startup fails with a missing-bean error. If they leave two, it fails with a non-unique-bean error. Test both the profile-on and profile-off cases. - Profile names with odd characters. Names should use letters, digits,
-and_. Avoid starting a name with-or!, and avoid names that contain&,|,(or), since those are reserved for profile expressions. - Setting
spring.profiles.activeinside a profile-specific document. Spring Boot rejects this, because the active profiles must be known before profile-specific documents are chosen. Set it in a non-profile source. - Putting production secrets in a committed
application-prod.yaml. A profile selects configuration. It does not protect it. The secrets chapter covers this.
Summary of what to remember
- A profile is a label. It does nothing until configuration or beans refer to it.
defaultis used only when no other profile is active.- Set
spring.profiles.activeby command line, environment variable, system property or file. Higher-precedence sources win, and the value is replaced rather than merged. - Use
spring.profiles.includeto add profiles without replacing the active list. - Use
@ActiveProfilesin tests.
Exercise
Your application.yaml contains spring.profiles.active: dev. You start the application with SPRING_PROFILES_ACTIVE=prod java -jar app.jar. Which profiles are active, and what happens to a bean annotated @Profile("default")?
Show answer
Only prod is active. The environment variable has higher precedence than the file, and spring.profiles.active is replaced, not merged. Because a non-default profile is active, default is not active, so the @Profile("default") bean is not registered.
Exercise
A @Configuration class defines Notifier beans with @Profile("!prod") and @Profile("prod"). The application is started with --spring.profiles.active=staging. How many Notifier beans exist, and which one?
Show answer
One. staging is not prod, so !prod matches and the logging notifier is registered. The prod bean is skipped.
Exercise
How would you activate both prod and metrics from an environment variable, and what would you use in a file if you wanted metrics always on without being replaced by the command line?
Show answer
Set SPRING_PROFILES_ACTIVE=prod,metrics. In a file, use spring.profiles.include: metrics, which adds the profile alongside whatever spring.profiles.active resolves to.