Profile Annotation
The @Profile
annotation in Spring Boot is a useful feature for defining beans that should only be created when specific profiles are active. Here’s a concise guide to using it:
- Define Profiles:
First, set up your profiles in your
application.properties
orapplication.yml
file. For example:# application.properties spring.profiles.active=dev
or
# application.yml spring: profiles: active: dev
- Use @Profile Annotation:
Apply the
@Profile
annotation to the beans that you want to activate only for certain profiles. For instance:import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @Component @Profile("dev") public class DevService { // Development-specific implementation } @Component @Profile("prod") public class ProdService { // Production-specific implementation }
- Activate Profiles:
To activate profiles, you can set the
spring.profiles.active
property. This can be done in theapplication.properties
orapplication.yml
file, or as a command-line argument:java -jar myapp.jar --spring.profiles.active=prod
- Conditional Bean Creation:
You can also use
@Profile
in configuration classes to conditionally create beans:import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration public class AppConfig { @Bean @Profile("dev") public DevService devService() { return new DevService(); } @Bean @Profile("prod") public ProdService prodService() { return new ProdService(); } }
That’s it! With the @Profile
annotation, you can easily manage different configurations and beans for different environments. Have fun coding! 🚀