Autoconfiguration in Spring Boot |Video upload date:  · Duration: PT46M5S  · Language: EN

Compact guide to how Spring Boot autoconfiguration works and how to customize and debug automatic bean wiring

What autoconfiguration does for you and why it sometimes misbehaves

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.

How Spring Boot decides which beans to create

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.

Common conditional annotations to know

  • @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 properties

How to take control without hating your life

If 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.

Debugging tips that will save you time

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.

Use Actuator for inspection

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.

Common traps and how to avoid them

  • Accidentally adding a starter pulls in an auto configuration you did not expect. Review transitive dependencies.
  • Relying too much on implicit wiring can hide lifecycle problems. Prefer explicit beans when initialization order matters.
  • Assuming a starter will always behave the same across versions can lead to surprises. Check the auto configuration class in the source when behavior looks magical.

Quick checklist when auto configuration surprises you

  • Run with --debug and read the autoconfiguration report in the logs
  • Inspect META-INF/spring.factories in your classpath to see which classes are candidates
  • Read the conditional annotations on the auto configuration class to know what it needs
  • Override the bean by declaring your own when behavior must be controlled
  • Use spring.autoconfigure.exclude or @EnableAutoConfiguration(exclude = ...) to prevent specific auto configurations

Autoconfiguration 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.