@Profile restricts a Spring bean or configuration class to specific named profiles, so it is registered only when one of those profiles is active. It is how you keep environment-specific wiring, such as an in-memory database for development and a real one for production, in one codebase without editing code per environment.
Beans without @Profile are always registered. When no profile is active, Spring uses a profile named default.
A profile is a named group of bean definitions. At startup Spring reads the active profiles from the Environment and registers only the matching @Profile beans. You can place the annotation on:
@Component, @Service, @Repository or @Configuration class.@Bean method inside a configuration class.With dev active, ConsoleMailSender is the only MailSender, and injection works without ambiguity. With prod active, SmtpMailSender is used. If neither profile is active, no MailSender exists and injection fails, so provide a default or use the default profile for local runs.
On a configuration class or a @Bean method:
Since Spring 5.1 the value can be a profile expression, with ! (not), & (and) and a pipe (or). You cannot mix & and | without parentheses.
| Expression | Registered when |
|---|---|
@Profile("dev") | dev is active |
@Profile("!prod") | prod is not active |
@Profile({"dev", "test"}) | dev or test is active |
@Profile("dev & cloud") | both dev and cloud are active |
@Profile("!cloud & !test") | neither cloud nor test is active |
There are several ways to set the active profiles. The first match in Spring's property precedence wins, with command-line and environment settings overriding files.
spring.profiles.active=dev in application.properties.java -jar app.jar --spring.profiles.active=prod or the JVM option -Dspring.profiles.active=prod.SPRING_PROFILES_ACTIVE=prod, the usual choice in containers.SpringApplication.setAdditionalProfiles("dev") or ConfigurableEnvironment.setActiveProfiles(...).@ActiveProfiles("test") on the test class.Several profiles are separated by commas, for example dev,debug. The spring.profiles.default property changes the name of the fallback profile.
Spring Boot loads application-{profile}.properties or .yml files for each active profile and lets them override the base file. In a single multi-document YAML file use spring.config.activate.on-profile, which replaced the removed spring.profiles key in Boot 2.4:
Boot also supports profile groups, so one name can activate several profiles:
Running with production then activates all three. Note that spring.profiles.active cannot be set inside a profile-specific document.
@ConditionalOnProperty for feature toggles, because it expresses intent better than a profile.Spring activates the profile named default. Beans annotated @Profile("default") are registered, and beans without @Profile are always registered.
Yes. Separate them with commas, as in spring.profiles.active=dev,debug. A bean is registered if its profile expression matches the active set.
@Profile is a ready-made condition based on active profile names. @Conditional and its Spring Boot variants test arbitrary things such as properties, classes on the classpath or missing beans.
Add @ActiveProfiles("test") to the test class. Spring then loads application-test.yml and the beans marked for test.
@Profile registers beans only for the active environment, which keeps one codebase usable in development, testing and production. Activate profiles with properties, environment variables or @ActiveProfiles, use profile expressions for combinations, and keep the profile set small so the configuration stays readable.