Top 10 Spring Boot Conditional Annotations

In Spring Boot, conditional annotations allow you to conditionally apply configurations or beans based on certain conditions. These conditions are typically evaluated during the application’s startup phase, and if they are met, the annotated element (such as a bean or configuration) is included; otherwise, it’s skipped.

Spring Boot Conditional Annotations

Commonly Used Spring Boot Conditional Annotations

Here are some commonly used Spring Boot conditional annotations:

  • @ConditionalOnBean: This annotation is used to specify that a bean should only be created if another bean of a specific type is already present in the application context.
@Configuration
 public class MyAutoConfiguration {

     @ConditionalOnBean
     @Bean
     public MyService myService() {
         ...
     }

 }
  • @ConditionalOnMissingBean: This annotation is used to specify that a bean should only be created if another bean of a specific type is not already present in the application context.
@Configuration
 public class MyAutoConfiguration {

     @ConditionalOnMissingBean
     @Bean
     public MyService myService() {
         ...
     }

 }
  • @ConditionalOnClass: This annotation is used to specify that a bean should only be created if a certain class is present on the classpath.
@Configuration
public class MyConfiguration {

    @Bean
    @ConditionalOnClass({ org.springframework.jdbc.datasource.DriverManagerDataSource.class })
    public MyBean myBean() {
        return new MyBean();
    }

    public static class MyBean {
        public void doSomething() {
            System.out.println("MyBean is doing something.");
        }
    }
}
  • @ConditionalOnMissingClass: This annotation is the opposite of @ConditionalOnClass. It specifies that a component should be created only if a specified class is missing from the classpath.
@Configuration
public class MyConfiguration {

    @Bean
    @ConditionalOnMissingClass("com.example.MyLibrary")
    public MyBean myBean() {
        return new MyBean();
    }
}
  • @ConditionalOnProperty: This annotation is used to specify that a bean should only be created if a certain property is set to a specific value.
@Configuration
public class MyFeatureConfiguration {

    @Bean
    @ConditionalOnProperty(
        name = "myapp.feature.enabled",
        havingValue = "true",
        matchIfMissing = true
    )
    public MyFeatureService myFeatureService() {
        return new MyFeatureService();
    }
}
  • @ConditionalOnResource: This annotation is used to specify that a bean should only be created if a certain resource is present.
@Configuration
@ConditionalOnResource(resources = "myconfig.properties")
public class MyConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}
  • @ConditionalOnWebApplication: This annotation is used to specify that a bean should only be created if the application is a web application.
@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class MyServletConfig {

    @RestController
    static class MyController {
        @GetMapping("/hello")
        public String hello() {
            return "Hello from Servlet-based application!";
        }
    }
}

Now, the MyServletConfig configuration class and its associated controller will only be active when your Spring Boot application is configured as a Servlet-based web application.

  • @ConditionalOnNotWebApplication: Similar to @ConditionalOnWebApplication, but creates components when the application is not a web application.
@Configuration
@ConditionalOnNotWebApplication
public class MyNonWebConfig {

    @Bean
    public MyNonWebComponent myNonWebComponent() {
        return new MyNonWebComponent();
    }

    static class MyNonWebComponent {
        void doSomething() {
            System.out.println("Doing something for non-web applications.");
        }
    }
}
  • @ConditionalOnJava : This annotation in Spring Boot allows you to conditionally enable or disable a bean or configuration class based on the version of the Java Runtime Environment (JRE) being used. You can specify the desired Java version or version range as a condition for the bean to be created.
@Configuration
public class MyConfiguration {

    @Bean
    @ConditionalOnJava(range = ConditionalOnJava.Range.EQUAL_OR_NEWER, value = ConditionalOnJava.JavaVersion.EIGHT)
    public MyBean myBean() {
        return new MyBean();
    }

    public static class MyBean {
        public void doSomething() {
            System.out.println("MyBean is doing something.");
        }
    }
}

This bean should be created if the Java version is equal to or newer than Java 8.

  • @ConditionalOnExpression: This annotation in Spring Boot allows you to conditionally enable or disable a bean or configuration class based on a custom SpEL (Spring Expression Language) expression. If the specified expression evaluates to true, the bean or configuration class is enabled; otherwise, it’s disabled.
@Configuration
public class MyConfiguration {

    @Bean
    @ConditionalOnExpression("${myapp.feature.enabled:true}")
    public MyBean myBean() {
        return new MyBean();
    }

    public static class MyBean {
        public void doSomething() {
            System.out.println("MyBean is doing something.");
        }
    }
}
  • If the property myapp.feature.enabled is not defined in your application’s properties, it defaults to true. Therefore, the myBean() method will create an instance of MyBean, and it will be available for use.
  • If you define the property myapp.feature.enabled in your application’s properties file and set it to false, the myBean() method will not create the bean, as the expression ${myapp.feature.enabled:true} will evaluate to false.

Conclusion : Spring Boot Conditional Annotations

Spring Boot Conditional Annotations provide a powerful way to selectively apply configurations and beans based on specific conditions. These annotations are evaluated during the application’s startup phase, enabling you to control which elements are included and which are skipped.

Leave a Reply

Your email address will not be published. Required fields are marked *