Sub topic 11
Choosing beans per profile with @Profile
This chapter assumes you can write a Spring @Configuration class and inject beans. It also assumes you know how to activate a profile, as covered in the earlier chapters on profiles and profile-specific files.
Why select beans by profile
Properties change values. Sometimes an environment needs a different implementation instead. In development you may want to print notifications to the console. In production you want to send real email. The @Profile annotation tells Spring to register a bean only when a given profile is active.
Marking beans with @Profile
Put @Profile on a @Bean method, or on a whole @Configuration or @Component class. The example below defines one Notifier per environment.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
interface Notifier {
void send(String message);
}
@Configuration
class NotifierConfig {
@Bean
@Profile("!prod")
Notifier loggingNotifier() {
return message -> System.out.println("[dev] " + message);
}
@Bean
@Profile("prod")
Notifier emailNotifier(MailProperties mail) {
return message -> System.out.println("Sending via " + mail.host() + ": " + message);
}
}Exactly one Notifier exists at runtime. With no profile, or any profile other than prod, you get loggingNotifier. With prod active, you get emailNotifier. The emailNotifier method takes MailProperties as a parameter, so Spring injects the validated properties from the earlier chapter.
Profile expressions
The profile name can be an expression. These are the forms you will use most.
| Expression | Bean is registered when | Column |
|---|---|---|
| `"prod"` | `prod` is active | |
| `"!prod"` | `prod` is not active | |
| `"dev \ | test"` | `dev` or `test` is active |
| `"cloud & eu"` | both `cloud` and `eu` are active |
You cannot mix & and | in one expression without parentheses. Write "(dev | test) & local" instead.
Common errors
- No qualifying bean. If no active profile matches any
Notifier, injection fails at startup withNoSuchBeanDefinitionException. Always cover the case where no profile is set, which is why!prodis used above instead ofdev. - Two beans match. If you wrote
@Profile("dev")and@Profile("!prod"), both match whendevis active, and injection fails withNoUniqueBeanDefinitionException. Keep the conditions mutually exclusive. - Class-level and method-level mismatch. A
@Profileon the class applies to every@Beanmethod inside it. A method cannot be enabled if its class is excluded.
Exercise
Exercise
A bean is annotated @Profile("dev") and another bean of the same type is annotated @Profile("!prod"). The application starts with --spring.profiles.active=dev. What happens when a class injects that type, and how do you fix it?
Show answer
Both conditions are true, because dev is active and prod is not. Two beans of the same type are registered, so injection fails with NoUniqueBeanDefinitionException. Fix it by making the conditions mutually exclusive, for example @Profile("dev") and @Profile("!dev"), or by marking one bean @Primary.