skip to content

What is a Spring profile and how do you activate one?

level: juniorimportance: must knowfreq 80%

answer

  1. Profile = named on/off switch for beans + config
  2. spring.profiles.active turns them on
  3. @Profile on @Bean/@Configuration
  4. No active -> default profile
  5. application-{profile}.yaml auto-loaded

basics

~10 s

A profile is a named group of beans and config that is only active in certain environments (like dev or prod). You turn it on with the property spring.profiles.active, e.g. spring.profiles.active=dev.

solid answer

~40 s

A Spring profile is a named logical grouping that lets you register different beans and load different configuration for different environments (dev, test, prod). You mark beans or config classes with @Profile("dev") so they only exist when that profile is active. You activate profiles via the spring.profiles.active property, which can be set as a JVM arg (-Dspring.profiles.active=prod), an environment variable (SPRING_PROFILES_ACTIVE=prod), a command-line arg (--spring.profiles.active=prod), or in application.yaml. Multiple profiles are comma-separated. If none is set, Spring uses the default profile (spring.profiles.default, itself defaulting to 'default'). Boot also auto-loads application-{profile}.yaml/properties files matching the active profile, layering them on top of the base application.yaml.

code

java · 20 lines
java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
class DataSourceConfig {

    @Bean
    @Profile("dev")
    DataSource devDataSource() {
        return new EmbeddedH2DataSource();
    }

    @Bean
    @Profile("prod")
    DataSource prodDataSource() {
        return new HikariDataSource(/* real DB */);
    }
}
// Activate: java -Dspring.profiles.active=prod -jar app.jar

go deeper

for a junior

Must know profiles are per-environment switches and that spring.profiles.active turns them on.

for a middle

Should know precedence of ways to set it, comma-separated multi-profile, and profile-specific files.

for a senior

Knows @Profile expression grammar, default profile semantics, and that active can't be set from a profile-specific document.

for a principal

Frames profiles as a deployment/config strategy; weighs profiles vs. externalized config, avoids profile explosion.

### What a profile is A **profile** is just a **name** (a String like `dev`, `test`, `prod`) that you attach to beans and configuration. When that name is in the set of *active* profiles, the associated beans get created and the associated config gets loaded; otherwise they are skipped entirely. Profiles let one codebase behave differently per environment without code changes. ### Marking things with `@Profile` The `@Profile` annotation (package `org.springframework.context.annotation`) goes on `@Component`/`@Service`/`@Configuration` classes or on `@Bean` methods: ```java @Bean @Profile("prod") DataSource prodDataSource() { ... } ``` That bean only enters the `ApplicationContext` when `prod` is active. `@Profile` supports a simple expression grammar: `@Profile("!prod")` (NOT), `@Profile({"dev","test"})` (OR — active if *any* listed profile is on), and `@Profile("prod & eu")` (AND, string form). ### Activating profiles: `spring.profiles.active` The active set is controlled by the property **`spring.profiles.active`**. Common ways to set it, in typical precedence order (later wins): - JVM system property: `-Dspring.profiles.active=prod` - OS environment variable: `SPRING_PROFILES_ACTIVE=prod` (relaxed binding — dots/underscores) - Command-line argument: `--spring.profiles.active=prod` - In `application.yaml`: `spring.profiles.active: dev` - Programmatically: `SpringApplication.setAdditionalProfiles(...)` or `ConfigurableEnvironment.setActiveProfiles(...)` Multiple profiles are comma-separated: `spring.profiles.active=prod,metrics`. ### The default profile If `spring.profiles.active` is empty/unset, the **default profile** applies. Its name is controlled by `spring.profiles.default` and is literally `default` unless you change it. `@Profile("default")` beans are active precisely when no other profile is active. ### Profile-specific config files Spring Boot automatically loads `application-{profile}.yaml`/`.properties` (e.g. `application-dev.yaml`) when `{profile}` is active, layering it *over* the base `application.yaml`. Profile-specific values win over base values. ### Common gotchas - `spring.profiles.active` set **inside** a profile-specific document is ignored — you cannot activate a profile from within a profile-specific file (Boot 2.4+). Use `spring.profiles.group` or `spring.profiles.include` instead. - Adding a profile via `SpringApplication.setAdditionalProfiles` *adds* to, rather than replaces, the active set. - Tests use `@ActiveProfiles("test")` to set the active profiles for the test context.

  • How do you set the active profile without changing code or the jar?
    Via an environment variable SPRING_PROFILES_ACTIVE, a JVM system property -Dspring.profiles.active, or a command-line arg --spring.profiles.active. These externalize the choice so the same artifact runs in any environment.
  • What happens to a @Profile("dev") bean when prod is active?
    It is not registered in the ApplicationContext at all — as if it didn't exist. Any injection point requiring it would fail to satisfy unless another bean of that type is active.

saying these in an interview costs you the question

  • Thinking @Profile beans are still created but 'disabled' rather than not registered at all
  • Believing you can set spring.profiles.active from inside a profile-specific file
  • Confusing spring.profiles.active with spring.profiles.default

context