# How @SpringBootApplication and Auto-Configuration Work

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

Canonical: https://www.internhack.in/learn/interview/spring-boot-interview/spring-boot-autoconfiguration-springbootapplication
Last updated: 2026-11-28

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

- @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan.
- Boot 3 registers auto-configurations in AutoConfiguration.imports, not spring.factories.
- Use --debug to print the conditions evaluation report.
- ConditionalOnMissingBean is why defining your own bean overrides the default.

## Common follow-up questions

- How would you create your own Spring Boot starter?
- How do you find out why a bean was or was not auto-configured?
- What is the difference between @Configuration and @AutoConfiguration?

## Interview tips

- Explain conditional annotations, not just that auto-configuration exists.
- Mention the debug report to show real troubleshooting experience.

## Concepts covered

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

Previous: [@Autowired, @Qualifier and @Primary](https://www.internhack.in/learn/interview/spring-boot-interview/spring-autowired-qualifier-primary) · Next: [Spring Boot Starters and Dependency Management](https://www.internhack.in/learn/interview/spring-boot-interview/spring-boot-starters) · [All of Spring Boot](https://www.internhack.in/learn/interview/spring-boot-interview)

## More Spring Boot questions

- [application.properties, YAML and Profiles](https://www.internhack.in/learn/interview/spring-boot-interview/spring-properties-yaml-profiles)
- [@RestController vs @Controller](https://www.internhack.in/learn/interview/spring-boot-interview/spring-restcontroller-vs-controller)
- [@PathVariable vs @RequestParam vs @RequestBody](https://www.internhack.in/learn/interview/spring-boot-interview/spring-request-mapping-pathvariable-requestparam-requestbody)
- [Bean Validation with @Valid and @Validated](https://www.internhack.in/learn/interview/spring-boot-interview/spring-validation-valid)
- [Global Exception Handling with @ControllerAdvice](https://www.internhack.in/learn/interview/spring-boot-interview/spring-controlleradvice-global-exception-handling)
- [ResponseEntity and Correct HTTP Status Codes](https://www.internhack.in/learn/interview/spring-boot-interview/spring-responseentity)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Spring Boot](https://www.internhack.in/learn/interview/spring-boot-interview)
