Conditional Beans in spring boot application

Introduction

As per spring-boot documentation, auto-configuration is implemented with standard @Configuration classes. Additional @Conditional annotations are used to constrain when the auto-configuration should apply. Usually, auto-configuration classes use @ConditionalOnClass and @ConditionalOnMissingBean annotations. This ensures that auto-configuration applies only when relevant classes are found and when you have not declared your own @Configuration.

Spring introduced the @Conditional annotation that allows users to define custom conditions to apply to parts of application context. Spring Boot builds on top of that and provides pre-defined conditions so we don’t need to implement them.

Spring-boot @Conditional will allow flexibility to enable/disable modules or beans into the application context if the required conditions met.

Conditional Annotations

Spring Boot includes a number of @Conditional annotations that we can reuse in our code by annotating @Configuration classes or individual @Bean methods. These annotations include:

  1. Class Conditions
  2. Bean Conditions
  3. Property Conditions
  4. Resource Conditions
  5. Web Application Conditions
  6. SpEL Expression Conditions

Please read reference spring-boot documentation of conditional annotations.

image url
img logo

No comments :

Post a Comment