Posts

Showing posts with the label Spring Boot Annotations

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

Spring Boot @ConditionalOnResource Annotation Example

Image
In this section we will learn about  @ConditionalOnResource Annotation. The  @ConditionalOnResource  annotation allows you to enable configuration only when a specific resource is available. We can specify resource's location of classpath as well as system locations. The  @ConditionalOnResource  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnProperty on @Bean method and @Configuration class For example,  The EmailNotificationService class is only loaded if the notification configuration file ( notification.properties ) was found on the classpath. The TwitterNotificationService class is only loaded if the notification and message configuration files ( notification.properties and message.properties ) was found on the  classpath . @Configuration @ConditionalOnResource (resources = { "notification.properties" }) public class AppConfig { @Bean ( &

Spring Boot @ConditionalOnProperty Annotation Example

Image
In this section we will learn about @ConditionalOnProperty  Annotation. The  @ConditionalOnProperty  annotation allows us to enable configuration based on the Spring Environment property. Use the prefix and name attributes to specify the property to validate. By default, any property that exists and is not false is mapped. We can also create more complex checks using the havingValue and matchIfMissing attributes. It may be useful in many cases for example enable/disable service if specific property is available. The  @ConditionalOnProperty  annotation may be used on any class annotated with @Configuration ,  @Component ,  @Service  &  @Repository  or on methods annotated with  @Bean . 1. Using @ConditionalOnProperty on @Bean method To illustrate the use of @ConditionalOnProperty , we will develop a basic notification system. To keep things simple for now, let's assume we want to send email notifications. Suppose we have a  key = notification.service and value =  twitter in  th