Cheatsheet

Auto-configuration

Key idea

Auto-configurations are ordinary `@Configuration` classes, listed in `META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports`, whose beans are only created if some conditions hold (`@ConditionalOnClass`, `@ConditionalOnMissingBean`, `@ConditionalOnProperty`…). That's how Boot configures the usual things and steps aside as soon as you declare your own.

Diagram (UML)

ApplicationContext@EnableAutoConfigurationAutoConfiguration.importsScoreboardAutoConfigurationcondition reportclasspath@ConditionalOn…your own ScoreboardConsoleScoreboardJdbcScoreboard (skipped)ScoreboardController

Key pieces

  • @EnableAutoConfiguration (inside @SpringBootApplication)
  • @AutoConfiguration (with before/after for ordering) and the AutoConfiguration.imports file
  • @ConditionalOnClass / @ConditionalOnMissingClass, @ConditionalOnBean / @ConditionalOnMissingBean
  • @ConditionalOnProperty(prefix, name, havingValue, matchIfMissing), @ConditionalOnWebApplication, @ConditionalOnResource
  • exclude / excludeName on @SpringBootApplication, spring.autoconfigure.exclude
  • debug=true / --debug → condition evaluation report; Actuator's conditions endpoint
  • ApplicationContextRunner, WebApplicationContextRunner, FilteredClassLoader for testing

ScoreboardAutoConfiguration.java

@AutoConfiguration                                       // listed in META-INF/spring/…AutoConfiguration.imports
@ConditionalOnProperty(prefix = "box.scoreboard", name = "enabled",
                       havingValue = "true", matchIfMissing = true)  // on unless box.scoreboard.enabled=false
public class ScoreboardAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean                            // back off if the app defines its own Scoreboard
    public Scoreboard scoreboard(@Value("${box.scoreboard.title:Scoreboard}") String title) {
        return new ConsoleScoreboard(title);
    }
    // …
}

Remember for the exam

  • Auto-configuration is processed after your configuration: that's how @ConditionalOnMissingBean knows whether you already declared the bean
  • Back-off: if you define your own bean of the type, the auto-configured one isn't created
  • @ConditionalOnClass looks at the classpath: adding a starter turns auto-configurations on; removing it, off
  • Since Boot 2.7/3.0 auto-configurations are registered in AutoConfiguration.imports; before, in spring.factories
  • The condition report (debug=true) lists positive matches, negative matches, exclusions and unconditional classes
  • A custom starter usually has an …-autoconfigure module with the code and an empty …-starter that brings it with its dependencies

Exam traps

  • Auto-configuration classes must not sit in a package your @ComponentScan covers: they'd load as regular configuration, ignoring the ordering
  • @ConditionalOnBean depends on evaluation order: in auto-configurations, order them with after/@AutoConfigureAfter
  • In a class with nested classes, the nested ones are processed before the outer class's @Beans: a @ConditionalOnBean on a nested one doesn't see those beans
  • @EnableWebMvc or your own DataSource switch off (by back-off) whole parts of auto-configuration

← Auto-configuration · Run it