Spring Boot autoconfiguration is the thing that wires up commonsense defaults so you can avoid writing boilerplate. It reads auto configuration classes declared in META-INF/spring.factories
and then evaluates conditional annotations like @ConditionalOnClass
and @ConditionalOnMissingBean
to decide which beans to create. The rules are simple and deterministic. The result is convenience, and occasionally the feeling that your framework has a personality.
The mechanism boils down to two checks. First it checks the classpath to see if a supporting library is available. Second it checks whether a bean is already present in the context. If the required class is on the classpath and no custom bean is present then the auto configuration will create a bean. This is why pulling in a starter dependency can suddenly add a bean to your app without a screaming error.
@ConditionalOnClass
checks whether a type exists on the classpath@ConditionalOnMissingBean
ensures a default is only created if you did not define your own@ConditionalOnProperty
enables or disables parts of auto configuration based on propertiesIf auto configuration chooses the wrong thing for you there are clean ways to override it. The easiest is to declare your own bean. Explicit bean definitions win over auto configured beans, and that is by design. If you need to stop classes from loading at all you can exclude specific auto configuration entries from @EnableAutoConfiguration(exclude = { ... })
or list their fully qualified names in the property spring.autoconfigure.exclude
. Use starters for convenience and switch to explicit wiring when you need precise control.
Guessing is a waste of oxygen. Start your app with --debug
to get an autoconfiguration report in the startup log that shows matched and unmatched conditions. If you like spelunking, grep META-INF/spring.factories
in your dependencies to find candidate auto configuration classes and then read the conditional annotations in the source to learn what is required on the classpath.
The Spring Boot Actuator can surface beans and property values so you can inspect what was added by auto configuration. Endpoints such as the beans and configprops pages are handy for spotting unwanted wiring and default values that came from starters.
--debug
and read the autoconfiguration report in the logsMETA-INF/spring.factories
in your classpath to see which classes are candidatesspring.autoconfigure.exclude
or @EnableAutoConfiguration(exclude = ...)
to prevent specific auto configurationsAutoconfiguration is a feature, not a mystery. Treat it like a helpful but sassy assistant. When it does something unexpected you now have the tools to investigate and fix it instead of muttering angry things at your build file.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.