How @SpringBootApplication and Auto-Configuration Work

Difficulty: Advanced

Question

What does @SpringBootApplication actually do, and how does auto-configuration decide which beans to create?

Answer

Let's peel back the magic, because this is the question that separates people who have used Spring Boot from those who understand it. @SpringBootApplication looks like one annotation, but it is a shortcut combining three: @SpringBootConfiguration, which is @Configuration in disguise and marks the class as a source of bean definitions; @ComponentScan, which scans the current package and sub-packages for stereotypes; and @EnableAutoConfiguration, which is where the interesting work happens.

@EnableAutoConfiguration imports a selector that reads a file listing candidate configuration classes. In Boot 2.7 and earlier it was META-INF/spring.factories; in Boot 3 it is META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports inside spring-boot-autoconfigure and any other library that ships its own. That file names hundreds of classes such as DataSourceAutoConfiguration, JacksonAutoConfiguration, and WebMvcAutoConfiguration. Each one is a normal @Configuration class, but guarded by conditions.

The conditions are the brain. @ConditionalOnClass says apply this config only if a certain class is on the classpath, for example only if Hibernate is present. @ConditionalOnMissingBean says create this bean only if the user has not defined one, which is what makes auto-configuration back off when you provide your own. @ConditionalOnProperty checks a property value, @ConditionalOnWebApplication checks the app type, and there are a few more. So the picture is: Boot ships a huge menu of defaults, and each item says, I only appear if these ingredients exist and you have not already ordered your own.

Concrete example: add spring-boot-starter-data-jpa and the H2 driver. DataSourceAutoConfiguration sees a DataSource class and an embedded database on the classpath, and with no URL configured it builds an in-memory H2 DataSource. HibernateJpaAutoConfiguration then builds an EntityManagerFactory on top of it, and JpaBaseConfiguration adds a transaction manager. Now define your own DataSource bean, and the conditional on missing bean makes Boot step aside.

How do you see what happened? Run with --debug or set debug=true to print the conditions evaluation report, which lists positive matches and negative matches with reasons. The Actuator conditions endpoint shows the same. To turn off a specific auto-configuration, use @SpringBootApplication(exclude = X.class) or the spring.autoconfigure.exclude property. That is your standard escape hatch when a starter configures something you do not want, for instance excluding DataSourceAutoConfiguration in a service that does not use a database.

Order also matters. Auto-configurations are processed after your own configuration, which is why user beans are visible to the conditional checks and win. You can create your own starter by writing a @AutoConfiguration class with conditions and registering it in the imports file. That is a nice closing point: it shows you understand that auto-configuration is not a Spring Boot secret, just an ordinary use of conditional configuration at scale.

Code examples

A custom conditional auto-configuration

@AutoConfiguration
@ConditionalOnClass(SmsClient.class)
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean          // backs off if the app defines its own SmsClient
    @ConditionalOnProperty(prefix = "sms", name = "enabled", havingValue = "true", matchIfMissing = true)
    public SmsClient smsClient(SmsProperties props) {
        return new SmsClient(props.getApiKey());
    }
}

// META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
// com.example.sms.SmsAutoConfiguration

// Application entry point equals three annotations in one
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class App { }

Conditions decide when a bean appears; ConditionalOnMissingBean is what lets your own bean override Boot's default.

Key points

Common follow-up questions

Interview tips

Concepts covered

SpringBootApplication, Auto-configuration, Conditional Annotations, AutoConfiguration.imports

More Spring Boot questions