Posts

Showing posts with the label Spring Boot Annotations

@DataR2dbcTest - Testing Spring Data R2DBC components

Image
In this section, we will learn how to test  Data R2DBC components  with @DataR2dbcTest in the Spring Boot application. About  @DataR2dbcTest Instead of bootstrapping the entire application context for every test,  @DataR2dbcTest  allows us to initialize only the parts of the Application context that are relevant to Data R2DBC components tests. Regular @Component , @Service or @Controller beans are not scanned when using this annotation.  This approach not only speeds up the testing process but also ensures a focused and efficient testing environment.  This approach is also known as " slicing " the application context. The annotation supports the following attributes: excludeAutoConfiguration :  Auto-configuration exclusions that should be applied for this test. excludeFilters : beans that would ordinarily be added to the application context can be filtered using a set of exclude filters. includeFilters : beans that would otherwise be filtered and added to the application e

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