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)
flowchart TB
app["@SpringBootApplication<br/>(@EnableAutoConfiguration)"] --> user["1 · your configuration<br/>(@ComponentScan, @Bean)"]
app --> imports["2 · META-INF/spring/…AutoConfiguration.imports<br/>(every jar)"]
imports --> auto["ScoreboardAutoConfiguration<br/>«@AutoConfiguration»"]
auto --> c1{"@ConditionalOnProperty<br/>box.scoreboard.enabled != false"}
c1 -- no --> off["nothing created"]
c1 -- yes --> c2{"@ConditionalOnMissingBean<br/>Scoreboard already defined?"}
c2 -- yes --> backoff["back off: your bean wins"]
c2 -- no --> bean["ConsoleScoreboard"]
auto --> c3{"@ConditionalOnWebApplication<br/>+ @ConditionalOnClass DispatcherServlet"}
c3 -- yes --> web["ScoreboardController"]
auto --> c4{"@ConditionalOnClass JdbcTemplate"}
c4 -- no --> skip["skipped (negative match)"]
auto -. "debug=true" .-> report["condition evaluation report"]Key pieces
@EnableAutoConfiguration(inside@SpringBootApplication)@AutoConfiguration(withbefore/afterfor ordering) and theAutoConfiguration.importsfile@ConditionalOnClass/@ConditionalOnMissingClass,@ConditionalOnBean/@ConditionalOnMissingBean@ConditionalOnProperty(prefix, name, havingValue, matchIfMissing),@ConditionalOnWebApplication,@ConditionalOnResourceexclude/excludeNameon@SpringBootApplication,spring.autoconfigure.excludedebug=true/--debug→ condition evaluation report; Actuator'sconditionsendpointApplicationContextRunner,WebApplicationContextRunner,FilteredClassLoaderfor 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
@ConditionalOnMissingBeanknows whether you already declared the bean - Back-off: if you define your own bean of the type, the auto-configured one isn't created
@ConditionalOnClasslooks 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, inspring.factories - The condition report (
debug=true) lists positive matches, negative matches, exclusions and unconditional classes - A custom starter usually has an
…-autoconfiguremodule with the code and an empty…-starterthat brings it with its dependencies
Exam traps
- Auto-configuration classes must not sit in a package your
@ComponentScancovers: they'd load as regular configuration, ignoring the ordering @ConditionalOnBeandepends on evaluation order: in auto-configurations, order them withafter/@AutoConfigureAfter- In a class with nested classes, the nested ones are processed before the outer class's
@Beans: a@ConditionalOnBeanon a nested one doesn't see those beans @EnableWebMvcor your ownDataSourceswitch off (by back-off) whole parts of auto-configuration