The @Profile annotation tells Spring to register a bean or configuration class only when a named profile is active. You use it to keep environment-specific wiring (a development database, a mock payment client, a production cache) in the same code base and switch between them with configuration instead of code changes.
A profile is a named, logical group of bean definitions. If no profile is active, Spring activates the profile named default, and beans annotated with @Profile for any other name are skipped.
@Profile is a conditional annotation. While the container scans configuration classes and @Bean methods, it asks the Environment whether the profile expression matches the active profiles. If it does not match, the bean definition is never registered, so the class is not instantiated and cannot be injected anywhere.
You can place it on:
@Component, @Service, @Repository or other stereotype class@Configuration class, which gates every @Bean method inside it@Bean method@Production)The most common pattern is one interface with an implementation per environment.
With dev active, only LoggingMailSender exists in the context, so any class that injects MailSender receives it. With prod active, it receives SmtpMailSender.
You can also gate a whole configuration class, or individual @Bean methods when one class holds beans for several environments:
Since Spring 5.1 the value of @Profile can be a profile expression, not only a plain name. The operators are ! (not), & (and) and | (or). You cannot mix & and | in one expression without parentheses.
| Annotation | Matches when |
|---|---|
@Profile("dev") | dev is active |
@Profile("!prod") | prod is not active |
@Profile({"dev", "test"}) | dev or test is active (an array means OR) |
@Profile("cloud & !local") | cloud is active and local is not |
@Profile("(dev | test) & eu") | eu is active together with dev or test |
A custom composed annotation keeps the profile name in one place and avoids typos in string literals:
@Profile only declares a condition. Something else has to activate the profile. The usual options, from most to least common in Spring Boot applications:
spring.profiles.active, set in application.properties, as a command-line argument or as an environment variable:spring.profiles.default to change the profile used when none is active.ConfigurableEnvironment.setActiveProfiles(...) in code, before the context is refreshed.@ActiveProfiles("test") on a Spring test class.Several profiles can be active at once, separated by commas: --spring.profiles.active=prod,eu.
Spring Boot loads application-{profile}.properties (or .yml) on top of application.properties when that profile is active. A file named application-dev.properties is read only for dev, and its values override the base file.
For a single multi-document YAML file, use spring.config.activate.on-profile (Spring Boot 2.4 and later). The older spring.profiles key inside a document was removed in Boot 2.4.
Profile groups let one name activate several others, for example spring.profiles.group.prod=proddb,prodmq. You cannot activate other profiles from inside a profile-specific document, which is why groups exist.
In tests, select the profile with @ActiveProfiles so the test context matches the beans you want:
Inject Environment when you need the active profiles in code, for example to log them at startup:
Prefer Profiles.of(...) over the deprecated acceptsProfiles(String...) overload. Avoid branching on profiles inside business code. Select the implementation with @Profile and keep the logic itself profile-agnostic.
Spring activates the profile named default. Beans with @Profile("default") are registered and beans with @Profile("dev") are not. Beans without @Profile are always registered.
No. It only affects bean registration, so it is meaningful on classes that Spring registers and on @Bean methods. Placing it on an arbitrary method has no effect.
List them with commas, for example spring.profiles.active=prod,eu. Both profiles are then active and a bean marked @Profile("eu") is registered.
No. A profile selects behaviour, not secrets. Provide passwords and keys through environment variables or a secrets manager and reference them from the profile-specific configuration.
@Profile conditionally registers beans and configuration classes by profile name or profile expression. Activate profiles with spring.profiles.active, @ActiveProfiles in tests or programmatically, and pair them with application-{profile} files for environment-specific properties. Use !, & and | for expressions, and keep profile checks out of business logic.