Posts

Showing posts with the label @ConditionalOnClass

Spring Boot @ConditionalOnClass Annotation Example

Image
In this section we will learn about @ConditionalOnClass Annotation. The  @ConditionalOnClass  can be used to register a bean only when the given class(es) can be found on the class-path. We can define the fully qualified class name either via a String or via a Class<?> reference. For example, when providing library/framework classes to other developers, this annotation can be used to active certain beans only based on the existing of specific classes, e.g. driver-classes etc. The  @ConditionalOnClass  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnClass on @Bean method To illustrate the use of  @ConditionalOnClass , we will  create two simple service classes, SMSNotificationService class and  EmailNotificationService class. public class EmailNotificationService { public EmailNotificationService () { System . out .println( "Inside EmailN