skip to content

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

level: juniorimportance: must knowfreq 78%

answer

  1. label on beans, only loaded when active
  2. spring.profiles.active
  3. no profile set → 'default' profile
  4. @Profile = @Conditional(ProfileCondition)
  5. comma-separated multiple

basics

~10 s

A profile is a named group of beans/config that is only registered when active. Mark beans with @Profile("dev"). Activate by setting the property spring.profiles.active=dev (property file, env var, or JVM flag).

solid answer

~40 s

A profile is a logical name (e.g. dev, prod, test) you attach to beans or configuration so they are only created when that profile is active. You annotate a @Component or @Bean method with @Profile("prod"); the bean is skipped unless prod is among the active profiles. You activate profiles via the spring.profiles.active property, set through application.properties, the SPRING_PROFILES_ACTIVE environment variable, or a JVM system property (-Dspring.profiles.active=prod). Multiple profiles are comma-separated. If none are set, Spring falls back to the 'default' profile, so @Profile("default") beans act as fallbacks. This lets one codebase behave differently per environment without code changes, most commonly for datasources, mail senders, mock vs real clients, and logging.

code

java · 16 lines
java
@Configuration
public class MailConfig {

    @Bean
    @Profile("prod")
    MailSender smtpSender() {
        return new JavaMailSenderImpl(); // real SMTP
    }

    @Bean
    @Profile("!prod")   // any non-prod profile
    MailSender loggingSender() {
        return new ConsoleLoggingMailSender();
    }
}
// Run with: -Dspring.profiles.active=prod

go deeper

for a junior

Define profile as environment-specific bean groups; know @Profile and spring.profiles.active.

for a middle

Add the 'default' profile fallback, comma-separated multiples, and the various ways to set the property (env var, JVM flag, file).

for a senior

Mention it's @Conditional under the hood, that it gates bean registration not values, and testing via @ActiveProfiles.

for a principal

Frame profiles as one of several conditional strategies; discuss when @Profile vs @ConditionalOnProperty is the better boundary and the operational risks of profile sprawl.

## What a profile is A **profile** is just a *label* (a String like `dev`, `prod`, `test`) that you can attach to beans and configuration. Beans tagged with a profile are only added to the `ApplicationContext` when that profile is **active**. This is Spring's built-in mechanism for environment-specific wiring — one codebase, different beans per environment. ## Declaring profile-scoped beans Use the `@Profile` annotation (package `org.springframework.context.annotation`): - On a `@Component`/`@Configuration` class — the whole class is conditional. - On a `@Bean` factory method — just that bean is conditional. ```java @Configuration public class DataConfig { @Bean @Profile("prod") DataSource prodDs() { /* real pooled DS */ } @Bean @Profile("dev") DataSource devDs() { /* H2 in-memory */ } } ``` A bean **without** any `@Profile` is always registered, regardless of active profiles. ## Activating profiles The active set is driven by the property **`spring.profiles.active`**. Common ways to set it (highest precedence wins): - JVM system property: `-Dspring.profiles.active=prod` - OS environment variable: `SPRING_PROFILES_ACTIVE=prod` (relaxed binding — underscores/uppercase). - In `application.properties`/`application.yml`: `spring.profiles.active=prod` - Programmatically before refresh (see ConfigurableEnvironment). Multiple profiles are comma-separated: `spring.profiles.active=prod,cloud`. ## The 'default' profile If **no** active profile is set, Spring uses the **default profiles** — literally the profile named `default`. So `@Profile("default")` beans are the fallback that loads only when nobody activated anything else. You can rename the fallback via `spring.profiles.default`. ## How it works under the hood `@Profile` is a meta-annotation: it is itself annotated with `@Conditional(ProfileCondition.class)`. During bean registration, `ProfileCondition` asks the `Environment` whether any of the listed profiles are accepted. So profiles are a specialization of the general `@Conditional` mechanism. ## When to use Datasource per environment, mock vs real external clients, dev-only debug beans, seed-data loaders, disabling schedulers in tests. In tests, activate with `@ActiveProfiles("test")`. ## Gotchas - Profiles decide **whether a bean is created**, not property values — for values use property files (though profile-specific files like `application-prod.properties` combine both). - Setting `spring.profiles.active` inside a profile-specific document is not allowed in Spring Boot. - A misspelled profile name silently activates nothing useful — no error.

  • What happens if you don't set spring.profiles.active at all?
    No profile is active, so Spring applies the 'default' profiles (the profile literally named 'default'). Beans annotated @Profile("default") load; other @Profile beans are skipped; unannotated beans always load.
  • Does @Profile control property values or bean creation?
    Bean creation/registration. It decides whether a bean definition is added to the context. Property values come from PropertySources; profile-specific property files (application-prod.properties) are a separate, complementary mechanism.

saying these in an interview costs you the question

  • Claiming @Profile changes property values
  • Thinking beans without @Profile only load in the default profile
  • Saying a wrong/misspelled profile name throws an error
  • Believing you must recompile to switch environments

context