Auto-Configuration & Starters

Overview

This page explains what @SpringBootApplication actually does, what a starter is, and how Boot decides “should I create a default DataSource?”.

Official: Using auto-configuration.


Table of contents

  1. @SpringBootApplication = three annotations
  2. Starters
  3. Auto-configuration
  4. How Boot finds auto-config classes
  5. Conditional beans
  6. Your beans always win
  7. Turning off auto-config
  8. Debugging with --debug
  9. Interview questions
  10. Summary

1. @SpringBootApplication = three annotations

Combined annotation What it does
@Configuration This class can declare @Bean methods
@EnableAutoConfiguration Apply Boot’s built-in defaults that match my classpath
@ComponentScan Scan this package + sub-packages for components

One annotation, three jobs. That’s why a single file boots the whole app.


2. Starters

A starter is a Maven/Gradle dependency that bundles libraries the Spring team has tested together.

Starter Pulls in
spring-boot-starter-web Spring MVC + Jackson + embedded Tomcat
spring-boot-starter-data-jpa Spring Data JPA + Hibernate + transactions
spring-boot-starter-security Spring Security

One line in your build instead of picking 20 compatible versions yourself.


3. Auto-configuration

A large set of pre-written @Configuration classes shipped in spring-boot-autoconfigure.jar. Each one checks:

  • Is class X on the classpath? (e.g. Hibernate)
  • Did the user already define their own bean?
  • Is this a web app?

If yes → it creates beans you’d otherwise write by hand (DataSource, EntityManagerFactory, Jackson config…).

Key point: auto-config is just @Configuration + guards. It steps aside when you provide your own beans.


4. How Boot finds auto-config classes

Spring Boot 3 reads class names from:

META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

(Spring Boot 2 used spring.factories for the same job — still mentioned in migration questions.)


5. Conditional beans

Auto-config rarely says “always create this”. It says “only if…”.

Annotation Meaning
@ConditionalOnClass Only if this class is on the classpath
@ConditionalOnMissingBean Only if the user hasn’t defined their own
@ConditionalOnProperty Only if a property has a certain value
@ConditionalOnWebApplication Only if it’s a web app

6. Your beans always win

If you define your own DataSource bean, Boot’s @ConditionalOnMissingBean(DataSource.class) fails → the default doesn’t activate.

Boot tries to be helpful, but your explicit @Bean always overrides the default.


7. Turning off auto-config

Java:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DemoApplication { }

Properties:

spring.autoconfigure.exclude=com.example.SomeAutoConfiguration

Use sparingly — excluding the wrong thing makes the app start but silently miss features.


8. Debugging with --debug

java -jar myapp.jar --debug

Spring prints an auto-configuration report: for each candidate, which conditions matched. Fastest way to answer “why did Boot start Redis?”.


9. Interview questions

Q: What does @SpringBootApplication combine? A: @Configuration + @EnableAutoConfiguration + @ComponentScan.

Q: What is a starter? A: A dependency that bundles coordinated libraries for a feature (web, JPA, security).

Q: What is auto-configuration? A: Pre-packaged @Configuration that registers beans only when classpath and properties match conditions.

Q: Where does Boot 3 list auto-config classes? A: META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports.

Q: How do I disable one auto-config? A: @SpringBootApplication(exclude = ...) or spring.autoconfigure.exclude property.

Q: How do I see what auto-config decided? A: Run with --debug or expose the Actuator conditions endpoint.


10. Summary

Word Remember
Starter Curated dependency bundle
Auto-config Conditional default beans
Your @Bean Overrides any default
Debug --debug shows the conditions report

Next steps


Happy Learning

Next: Configuration, Profiles & Properties