0
0
Spring Bootframework~5 mins

Conditional bean creation in Spring Boot

Choose your learning style9 modes available
Introduction

Conditional bean creation helps your Spring Boot app create beans only when certain conditions are true. This keeps your app clean and efficient.

You want to create a bean only if a specific property is set in your configuration.
You want to load a bean only when running in a certain environment, like development or production.
You want to create a bean only if another bean is missing or present.
You want to enable a feature only when a certain class is available on the classpath.
Syntax
Spring Boot
@ConditionalOnProperty(name = "property.name", havingValue = "value")
@Bean
public MyBean myBean() {
    return new MyBean();
}
Use @ConditionalOnProperty to create beans based on config properties.
Spring Boot offers other conditions like @ConditionalOnMissingBean and @ConditionalOnClass.
Examples
This bean is created only if the property feature.enabled is set to true.
Spring Boot
@ConditionalOnProperty(name = "feature.enabled", havingValue = "true")
@Bean
public FeatureService featureService() {
    return new FeatureService();
}
This bean is created only if no other bean of the same type exists.
Spring Boot
@ConditionalOnMissingBean
@Bean
public DefaultService defaultService() {
    return new DefaultService();
}
This bean is created only if SomeClass is on the classpath.
Spring Boot
@ConditionalOnClass(name = "com.example.SomeClass")
@Bean
public OptionalService optionalService() {
    return new OptionalService();
}
Sample Program

This Spring Boot app creates the GreetingService bean only if the property app.greeting.enabled is set to true in application.properties. The main method checks if the bean exists and prints the result.

Spring Boot
package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        var context = SpringApplication.run(DemoApplication.class, args);
        boolean hasBean = context.containsBean("greetingService");
        System.out.println("Is GreetingService bean created? " + hasBean);
    }

    @Bean
    @ConditionalOnProperty(name = "app.greeting.enabled", havingValue = "true")
    public GreetingService greetingService() {
        return new GreetingService();
    }

    public static class GreetingService {
        public String greet() {
            return "Hello!";
        }
    }
}
OutputSuccess
Important Notes

Make sure to set the property in application.properties or application.yml to control bean creation.

Conditional annotations help keep your app flexible and avoid loading unnecessary beans.

Summary

Conditional bean creation lets Spring Boot create beans only when needed.

Use annotations like @ConditionalOnProperty to control when beans load.

This helps keep your app clean, fast, and easy to manage.