Spring Boot Auto-Configuration Internals Explained

Spring Boot auto-configuration is the feature that makes it so easy to get started. It automatically configures your application based on the libraries present on the classpath and your defined settings in application.properties or application.yml.

No XML. No manual wiring. Just focus on your business logic.

Example: If you add spring-boot-starter-web, Spring Boot automatically configures:

  • DispatcherServlet
  • Jackson (for JSON)
  • Tomcat (embedded)
  • MVC infrastructure
Spring Boot Auto-Configuration

🔍 How Does It Work Internally?

Auto-configuration uses:

  • Spring Factories
  • Conditional Annotations
  • Classpath detection
  • @EnableAutoConfiguration

Let’s break it down.

🏁 Step 1: @SpringBootApplication Enables Auto-Configuration

The @SpringBootApplication annotation includes:

Here, @EnableAutoConfiguration triggers the magic. It tells Spring Boot to look for configuration classes and apply them based on what’s available.

🧠 Step 2: META-INF/spring.factories

Spring Boot looks at:

This file contains entries like:

Spring loads each of these classes and checks if it should apply them using conditional annotations.

⚙️ Step 3: Conditional Configuration (The Brain Behind It)

Each auto-configuration class uses smart annotations to decide when to activate.

Common ones include:

AnnotationWhat It Checks
@ConditionalOnClassOnly loads if a class exists (e.g., DispatcherServlet)
@ConditionalOnMissingBeanLoads only if no other bean of that type is present
@ConditionalOnPropertyLoads if a property is set (or not)
@ConditionalOnWebApplicationLoads only for web applications
@ConditionalOnBeanLoads only if another bean is present

✅ This is why Spring Boot is “auto” — but still safe and controllable.

📦 Real-World Example: Spring Web Starter

If you add this dependency:

Then:

  • WebMvcAutoConfiguration gets triggered
  • It sees DispatcherServlet is on classpath
  • It configures Spring MVC automatically
  • You get a running web server without writing any config

🧰 Customizing Auto-Configuration

✅ Override a Bean

If Spring Boot autoconfigures a bean, but you define one manually, yours will take priority.

Example: To override Jackson’s object mapper:

❌ Disable Auto-Config

You can disable a specific auto-config using:

Or, via application.properties:

🔍 Debugging Auto-Configuration

Use Spring Boot Actuator or debug logs to see which configs were applied:

This will print lines like:

🛠️ Tools That Use Auto-Configuration

  • Spring Web
  • Spring Data JPA
  • Spring Security
  • Spring Cache
  • Spring Kafka
  • Spring Mail
  • Spring Actuator

Pretty much every starter uses this model.

🧪 Can I Write My Own Auto-Configuration?

Yes! You can create custom auto-configurations like this:

Then register it in:

🧭 Summary

FeaturePurpose
@EnableAutoConfigurationEntry point for auto-config
spring.factories / importsLists all auto-config classes
Conditional annotationsDecide whether to load config
application.propertiesFine-tune or disable configs
Manual BeansOverride defaults if needed

✅ Why You Should Learn It

  • Helps you debug startup issues
  • Lets you override Spring’s behavior safely
  • You’ll understand what’s going on “under the hood”
  • Essential for building custom Spring Boot starters

References :

Spring Boot Auto Configurations Docs