Posts

Showing posts with the label @ConditionalOnProperty

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