Custom auto-configuration helps your Spring Boot app set up beans automatically without manual setup. It saves time and keeps your code clean.
Custom auto-configuration in Spring Boot
@Configuration @ConditionalOnClass(SomeClass.class) @ConditionalOnProperty(prefix = "my.feature", name = "enabled", havingValue = "true", matchIfMissing = true) public class MyAutoConfiguration { @Bean public MyService myService() { return new MyService(); } }
@Configuration marks the class as a source of bean definitions.
@ConditionalOnClass creates beans only if a class is on the classpath.
@Configuration @ConditionalOnClass(DataSource.class) public class DataSourceAutoConfiguration { @Bean public DataSource dataSource() { return new HikariDataSource(); } }
feature.enabled=true is set.@Configuration @ConditionalOnProperty(prefix = "feature", name = "enabled", havingValue = "true") public class FeatureAutoConfiguration { @Bean public FeatureService featureService() { return new FeatureService(); } }
@Configuration @ConditionalOnMissingBean(MyService.class) public class MyServiceAutoConfiguration { @Bean public MyService myService() { return new MyService(); } }
This example shows a custom auto-configuration class that creates a MyService bean if the class is present and a property myapp.service.enabled is true or missing. The main app fetches the bean and prints a greeting.
package com.example.autoconfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @Configuration @ConditionalOnClass(MyService.class) @ConditionalOnProperty(prefix = "myapp.service", name = "enabled", havingValue = "true", matchIfMissing = true) public class MyServiceAutoConfiguration { @Bean @ConditionalOnMissingBean public MyService myService() { return new MyService(); } } // Simple service class class MyService { public String greet() { return "Hello from MyService!"; } } // Main application to test auto-configuration package com.example.demo; import com.example.autoconfig.MyService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args); MyService service = ctx.getBean(MyService.class); System.out.println(service.greet()); } }
Always use @ConditionalOnMissingBean to allow users to override your beans.
Put your auto-configuration classes in spring.factories or use the new spring.autoconfigure file for Spring Boot 3+.
Test your auto-configuration by enabling/disabling properties and checking bean presence.
Custom auto-configuration creates beans automatically based on conditions.
Use conditions like class presence and properties to control bean creation.
It helps make reusable, easy-to-configure Spring Boot modules.