Spring
June 6, 2020
M01 Q24 What is @Profile annotation?
How do you configure profiles?
Spring Profiles are configured by:
- Specifying which beans are part of which profile
- Specifying which profiles are active
You can specify beans being part of the profile in the following ways:
- Use
@Profile
annotation at@Component
class level – bean will be part of profile/profiles specified in the annotation
- Use
@Profile
annotation at@Configuration
class level – all beans from this configuration will be part of profile/profiles specified in the annotation
- Use
@Profile
annotation at@Bean
method of@Configuration
class – an instance of bean returned by this method will be part of profile/profiles specified in the annotation
- Use
@Profile
annotation to define custom annotation -@Component
/@Configuration
/@Bean
method annotated with custom annotation will be part of profile/profiles specified in the annotation
If Bean does not have profile specified in any way, it will be created in every profile. You can use !
to specify which profile bean should not be created.
How to activate profile?
You can activate profiles in the following way:
- Programmatically with the usage of
ConfigurableEnvironment
- By using
spring.profiles.active
property
- On JUnit Test level by using
@ActiveProfiles
annotation
- In Spring Boot Programmatically by the usage of
SpringApplicationBuilder
- In Spring Boot by
application.properties
or in YML-file
What are possible use cases where they might be useful?
Spring Profiles are useful in the following cases:
- Changing Behavior of the system in Different Environments by changing set of Beans that are part of specific environments, for example, prod, cert, dev
- Changing Behavior of the system for different customers
- Changing set of Beans used in Development Environment and also during Testing Execution
- Changing set of Beans in the system when monitoring or additional debugging capabilities should be turned on