Sub topic 9
Profiles: what they are and how to activate them
This chapter assumes you have a working Spring Boot application, know how properties files and @Value work, and understand property precedence from the earlier chapters.
What a profile is
A profile is a named label for an environment, such as dev, test or prod. Spring Boot keeps a set of active profile names. Other features read that set to decide which configuration and which beans to use. Later chapters cover those features. This chapter covers how names get into the set.
If you activate nothing, Spring Boot uses a profile called default. The startup log shows which case applies:
No active profile set, falling back to 1 default profile: "default"
The following 1 profile is active: "prod"Activating any profile replaces default. It does not add to it.
Ways to activate a profile
The property spring.profiles.active holds a comma-separated list of profile names. Because it is an ordinary property, it follows the normal precedence rules. You can set it in any property source.
# Command-line argument
java -jar app.jar --spring.profiles.active=prod
# Environment variable (relaxed binding)
SPRING_PROFILES_ACTIVE=prod,metrics java -jar app.jarYou can also set it in application.yaml:
spring:
profiles:
active: devThis sets a default for local work. A command-line argument or environment variable overrides it, so the same jar can run as prod without any edit.
In code, SpringApplication.setAdditionalProfiles adds profiles before startup. It adds to the profiles that properties activate, and it does not replace them.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setAdditionalProfiles("metrics");
app.run(args);
}
}Checking what is active
Inject Environment and print its profiles at startup:
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import java.util.Arrays;
@Configuration
class ProfileReport {
@Bean
CommandLineRunner printProfiles(Environment env) {
return args -> System.out.println(
"Active: " + Arrays.toString(env.getActiveProfiles()));
}
}If no profile is active, getActiveProfiles() returns an empty array. The default profile appears in getDefaultProfiles() instead.
Common errors
- A misspelled profile name does not cause an error. The profile is active, but nothing matches it, so you silently get the default configuration. Check the startup log line.
- Setting
spring.profiles.activeinside a profile-specific file or a document withspring.config.activate.on-profilefails at startup. Profiles must be chosen before profile-specific configuration is read, so set them in a general source. - Using
SPRING_PROFILES_ACTIVE=prodand--spring.profiles.active=devtogether activates onlydev, because the command line has higher precedence. Check each source if the result surprises you.
Exercise
An application.yaml sets spring.profiles.active to dev. You start the jar with SPRINGPROFILESACTIVE=prod and also pass --spring.profiles.active=test. Which profile is active, and why?
Show answer
Only test is active. The command-line argument has higher precedence than the environment variable, and the environment variable has higher precedence than application.yaml. The winning source replaces the value from the others. The values are not merged.