Profiles
The same application runs on your laptop, in CI, in a staging environment and in production — each with a different database, log level and set of integrations. Profiles let you keep one build and switch configuration and beans by naming the environment at startup.
Profile-specific property files
Next to application.properties, add files named application-<profile>.properties (or .yml):
src/main/resources/
├── application.properties always loaded
├── application-dev.properties only when "dev" is active
└── application-prod.properties only when "prod" is active# application.properties - shared defaults
spring.application.name=tasks
tasks.page-size=20
logging.level.root=INFO# application-dev.properties
spring.datasource.url=jdbc:h2:mem:tasks
logging.level.com.example.tasks=DEBUG
spring.jpa.show-sql=true# application-prod.properties
spring.datasource.url=jdbc:postgresql://db:5432/tasks
tasks.page-size=50Values from an active profile file override the same keys from the base file; everything else is inherited.
Activating profiles
| Way | Example |
|---|---|
| property in the base file (development default) | spring.profiles.active=dev |
| command-line argument | java -jar tasks.jar --spring.profiles.active=prod |
| environment variable | SPRING_PROFILES_ACTIVE=prod |
| Maven plugin | ./mvnw spring-boot:run -Dspring-boot.run.profiles=dev |
| in tests | @ActiveProfiles("test") |
Several profiles can be active at once: --spring.profiles.active=prod,metrics. The last one listed wins when they set the same key.
java -jar target/tasks-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod | grep -i profile2025-10-16T10:02:11.207+07:00 INFO 51544 --- [tasks] [ main] com.example.tasks.TasksApplication : The following 1 profile is active: "prod"
Without any active profile, Boot logs No active profile set, falling back to 1 default profile: "default".
How the files are merged
Here is the merge rule — base first, then each active profile in order, later values overwriting earlier ones:
import java.util.*;
class Main {
static Map<String, String> load(Map<String, Map<String, String>> files, List<String> active) {
Map<String, String> env = new TreeMap<>(files.get("application"));
for (String profile : active) {
Map<String, String> extra = files.get("application-" + profile);
if (extra != null) env.putAll(extra);
}
return env;
}
public static void main(String[] args) {
Map<String, Map<String, String>> files = Map.of(
"application", Map.of("tasks.page-size", "20", "logging.level.root", "INFO",
"spring.datasource.url", "jdbc:h2:mem:tasks"),
"application-prod", Map.of("tasks.page-size", "50",
"spring.datasource.url", "jdbc:postgresql://db:5432/tasks"),
"application-debug", Map.of("logging.level.root", "DEBUG"));
System.out.println("[] -> " + load(files, List.of()));
System.out.println("[prod] -> " + load(files, List.of("prod")));
System.out.println("[prod, debug] -> " + load(files, List.of("prod", "debug")));
}
}[] -> {logging.level.root=INFO, spring.datasource.url=jdbc:h2:mem:tasks, tasks.page-size=20}
[prod] -> {logging.level.root=INFO, spring.datasource.url=jdbc:postgresql://db:5432/tasks, tasks.page-size=50}
[prod, debug] -> {logging.level.root=DEBUG, spring.datasource.url=jdbc:postgresql://db:5432/tasks, tasks.page-size=50}
Several profiles in one YAML file
YAML supports multiple documents separated by ---. A document applies only when its spring.config.activate.on-profile matches:
tasks:
page-size: 20
---
spring:
config:
activate:
on-profile: prod
tasks:
page-size: 50Profile-specific beans
@Profile registers a bean only when a profile is (or is not) active:
public interface MailSender { void send(String to, String text); }
@Component
@Profile("prod")
class SmtpMailSender implements MailSender {
public void send(String to, String text) { /* real SMTP call */ }
}
@Component
@Profile("!prod") // every profile except prod
class ConsoleMailSender implements MailSender {
private static final Logger log = LoggerFactory.getLogger(ConsoleMailSender.class);
public void send(String to, String text) { log.info("MAIL to {}: {}", to, text); }
}Exactly one MailSender exists in any environment, so services can inject it without caring which one they get. Profile expressions support !, & and |, e.g. @Profile("prod & !eu").
import java.util.List;
import java.util.Set;
class Main {
// a tiny evaluator for "name" and "!name" profile conditions
static boolean matches(String expr, Set<String> active) {
return expr.startsWith("!") ? !active.contains(expr.substring(1)) : active.contains(expr);
}
public static void main(String[] args) {
record Candidate(String bean, String profile) {}
List<Candidate> beans = List.of(
new Candidate("SmtpMailSender", "prod"),
new Candidate("ConsoleMailSender", "!prod"));
for (Set<String> active : List.of(Set.of("dev"), Set.of("prod"), Set.<String>of())) {
for (Candidate c : beans) {
if (matches(c.profile(), active)) {
System.out.println("active=" + active + " -> MailSender is " + c.bean());
}
}
}
}
}active=[dev] -> MailSender is ConsoleMailSender
active=[prod] -> MailSender is SmtpMailSender
active=[] -> MailSender is ConsoleMailSender
Profile groups
A group activates several profiles with one name:
spring.profiles.group.prod=postgres,smtp,json-logsNow --spring.profiles.active=prod also activates postgres, smtp and json-logs.
Tip: Keep profiles few and meaningful (
dev,test,prod). Values that differ for every deployment — passwords, hostnames — belong in environment variables, not in a profile file committed to Git. The Configuration for Environments lesson goes deeper.
Test yourself
Profile prod is active. Which file's value wins for a key defined in both application.properties and application-prod.properties?
- application.properties
- application-prod.properties
- The alphabetically first
- Startup fails
What does @Profile("!prod") mean?
- Only in prod
- In every environment except when prod is active
- Never
- Prod is mandatory
Which environment variable activates the prod profile?
PROFILE=prodSPRING_PROFILES_ACTIVE=prodSPRING_ENV=prodJAVA_PROFILE=prod