Posts

Showing posts with the label Spring Boot Annotations

Spring Boot @ConditionalOnMissingClass Annotation Example

Image
In this section we will learn about @ConditionalOnMissingClass  Annotation. The  @ConditionalOnMissingClass  can be used to register a bean only when the given class(es) cannot be found on the class-path.  For example, when providing library/framework classes to other developers, this annotation can be used to active certain beans only based on the absence of specific classes. The  @ConditionalOnMissingClass  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnMissingClass on @Bean method To illustrate the use of  @ConditionalOnMissingClass , we will  create one simple service classe,  SMSNotificationService  class. public class SMSNotificationService { public SMSNotificationService() { System.out.println( "Inside SMSNotificationService Constructor" ); } public void sendSmsNotification() { System.out.println( "Sending S

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