Beans Conditional Creation

These annotations allow beans to be created only if certain conditions are met.

Here’s a breakdown with examples:


1. Using @Conditional

You can define your own condition by implementing Condition.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // Example: Register bean only if "my.feature.enabled" is true
        String enabled = context.getEnvironment().getProperty("my.feature.enabled");
        return "true".equalsIgnoreCase(enabled);
    }
}

@Configuration
public class MyConfig {

    @Bean
    @Conditional(MyCondition.class)
    public String conditionalBean() {
        return "Bean is loaded!";
    }
}

✅ If my.feature.enabled=true is in application.properties, the bean will be created.


2. @ConditionalOnProperty

Registers a bean if a property has a specific value.

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PropertyBasedConfig {

    @Bean
    @ConditionalOnProperty(name = "featureX.enabled", havingValue = "true", matchIfMissing = false)
    public String featureXBean() {
        return "Feature X Bean loaded";
    }
}
  • If featureX.enabled=true → Bean is created.
  • If property missing or false → Bean is skipped.

3. @ConditionalOnClass

Registers a bean only if a class is on the classpath.

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ClassBasedConfig {

    @Bean
    @ConditionalOnClass(name = "com.mysql.cj.jdbc.Driver")
    public String mysqlBean() {
        return "MySQL Bean loaded";
    }
}

✅ Bean loads only if MySQL JDBC driver is present in classpath.


4. @ConditionalOnMissingBean

Registers a bean only if another bean is not already defined.

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MissingBeanConfig {

    @Bean
    @ConditionalOnMissingBean(name = "customBean")
    public String defaultBean() {
        return "Default Bean";
    }
}

✅ This prevents overriding user-defined beans unless missing.


5. @ConditionalOnExpression

Registers a bean based on a SpEL (Spring Expression Language) condition.

import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ExpressionConfig {

    @Bean
    @ConditionalOnExpression("'${app.mode}'=='dev'")
    public String devBean() {
        return "Dev Mode Bean";
    }
}

✅ If app.mode=dev in properties → Bean is created.


Summary Table

AnnotationCondition
@ConditionalCustom logic
@ConditionalOnPropertyProperty exists with value
@ConditionalOnClassClass available on classpath
@ConditionalOnMissingBeanNo existing bean of type/name
@ConditionalOnExpressionSpEL evaluates true

Leave a Reply