Posts

Showing posts with the label @ConditionalOnBean

Spring Boot @ConditionalOnBean Annotation Example

Image
In this section we will learn about  @ConditionalOnBean   Annotation. The  @ConditionalOnBean  annotation let a bean be loaded based on the presence of specific bean inside Spring container. The  @ConditionalOnBean  annotation may be used on any class annotated with  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnBean on @Bean method For example,  @ConditionalOnBean (name = "emailNotificationProvider" ) , when the Bean of name  "emailNotificationProvider"  exists in the container, the bean  emailNotification  will be loaded. @Bean ( "emailNotification" ) @ConditionalOnBean (name = "emailNotificationProvider" ) public EmailNotificationService emailNotificationService () { return new EmailNotificationService(); } 2. Using @ConditionalOnBean on @Service class For example,  @ConditionalOnBean (name =  "smsNotificationProvider" ) , when the Bean of name  "smsNotificationProvi