If you expected magic then sorry, this is engineering dressed up as convenience. Spring Boot auto configuration is just metadata plus rules that decide which beans to register for you. @EnableAutoConfiguration flips the switch and the framework consults the metadata produced by starters and the classpath to figure out what to wire. No mysticism required, only a tiny bit of clever bureaucracy.
Starters and libraries ship metadata that lists candidate auto configuration classes. Historically that metadata lived in META-INF/spring.factories
and newer versions also use META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
. Spring Boot loads that list at startup and treats it like a menu of default configurations to consider.
Auto configuration classes are full of guard rails. Annotations such as @ConditionalOnClass
and @ConditionalOnMissingBean
act like bouncers at a club. They inspect the runtime environment and either let a configuration in or send it packing.
Typical checks you will see include presence of classes, missing beans, property values, and environment profiles. The outcome is that only relevant configs apply to your application context, which is neat unless you forgot to include a required dependency.
When a conditional passes Spring registers beans following the normal bean lifecycle. Spring Boot uses ordering hints such as @AutoConfigureBefore
and @AutoConfigureAfter
in addition to import ordering to avoid conflicts.
If you provide a user defined bean of the same type the framework respects that and usually does not register the auto configured alternative. In other words your bean often wins. That is how you customize behavior without hacking the framework.
There are polite and blunt ways to stop auto configuration from doing its thing.
spring.autoconfigure.exclude
property or with the exclude attribute on @EnableAutoConfiguration
.When behavior is mysterious enable the auto configuration report. Start your app with --debug
or set debug=true
in your properties. Spring Boot will print which auto configuration classes matched and which did not. You can also raise logging for org.springframework.boot.autoconfigure
to DEBUG for extra detail.
That report saves time and spares guesswork when the framework is behaving like a needy magician that only does tricks under specific conditions.
Auto configuration is metadata plus conditional rules plus ordering and overrides. Learn where the metadata comes from, read the conditions such as @ConditionalOnClass
and @ConditionalOnMissingBean
, and use exclusion or user beans when you want to steer the defaults. With a little practice you can keep the convenience and avoid surprises.
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.