TExcelTAD
Home › Spring Boot › Profiles

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):

text
src/main/resources/
├── application.properties          always loaded
├── application-dev.properties      only when "dev" is active
└── application-prod.properties     only when "prod" is active
properties
# application.properties - shared defaults
spring.application.name=tasks
tasks.page-size=20
logging.level.root=INFO
properties
# application-dev.properties
spring.datasource.url=jdbc:h2:mem:tasks
logging.level.com.example.tasks=DEBUG
spring.jpa.show-sql=true
properties
# application-prod.properties
spring.datasource.url=jdbc:postgresql://db:5432/tasks
tasks.page-size=50

Values 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.

bash · try it yourself
java -jar target/tasks-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod | grep -i profile
2025-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:

java · try it yourself
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:

yaml
tasks:
  page-size: 20
---
spring:
  config:
    activate:
      on-profile: prod
tasks:
  page-size: 50

Profile-specific beans

@Profile registers a bean only when a profile is (or is not) active:

java
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").

java · try it yourself
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:

properties
spring.profiles.group.prod=postgres,smtp,json-logs

Now --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?

  1. application.properties
  2. application-prod.properties
  3. The alphabetically first
  4. Startup fails

What does @Profile("!prod") mean?

  1. Only in prod
  2. In every environment except when prod is active
  3. Never
  4. Prod is mandatory

Which environment variable activates the prod profile?

  1. PROFILE=prod
  2. SPRING_PROFILES_ACTIVE=prod
  3. SPRING_ENV=prod
  4. JAVA_PROFILE=prod