Spring Boot: From Basics to Production · Spring Core and Spring Boot Fundamentals

Auto-configuration and Starters

Understand how Boot configures beans for you and how to override it.

Starters are dependency bundles (e.g., the web starter brings Spring MVC, Jackson and Tomcat). Auto-configuration classes create beans conditionally (@ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty) — for example a DataSource when a JDBC driver is on the classpath and a URL is configured. Defining your own bean of the same type usually makes Boot back off. Run with --debug to see the condition evaluation report.

// Override Boot's default ObjectMapper customization without replacing everything
@Configuration
class JacksonConfig {
    @Bean
    Jackson2ObjectMapperBuilderCustomizer customizer() {      // Boot 3.x API; Boot 4 uses Jackson 3 builder customizers
        return builder -> builder.failOnUnknownProperties(true);
    }
}

// Writing your own conditional auto-config (in a shared library)
@AutoConfiguration
@ConditionalOnClass(AuditClient.class)
@EnableConfigurationProperties(AuditProperties.class)
public class AuditAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    AuditClient auditClient(AuditProperties props) {
        return new AuditClient(props.endpoint());
    }
}

Register library auto-configurations in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.

Common pitfalls

  • Replacing a whole auto-configured bean when a customizer would do.
  • Excluding auto-configurations blindly to 'fix' startup errors.
  • Not checking the condition report when a bean is unexpectedly missing.

Key points

  • Starters bundle dependencies
  • Conditional beans back off when you define your own
  • --debug shows the condition report

Further reading: Spring Boot: Auto-configuration

Practice questions

How do you find out why an auto-configured bean wasn't created?

Answer

Start the app with --debug (or check the Actuator conditions endpoint) to see which conditions matched or didn't.

What does @ConditionalOnMissingBean do?

Answer

It creates the bean only if no bean of that type already exists, letting applications override library defaults by defining their own.

Open in the app to mark lessons complete, take notes and track your progress.