Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 3 OF 7 · 2 MIN READ

How Spring Boot Auto-Configuration Works

Spring Boot auto-configuration creates sensible beans based on what is on the classpath and in your properties. Interviewers ask what @SpringBootApplication does, how starters and conditional annotations work, and how to override a default.

What you will learn

  • Explain @SpringBootApplication
  • Explain starters and auto-configuration classes
  • Use @ConditionalOnClass, @ConditionalOnMissingBean and @ConditionalOnProperty
  • Debug which auto-configurations applied

Key concepts

@SpringBootApplication

Combines @SpringBootConfiguration, @EnableAutoConfiguration and @ComponentScan of the package and its sub-packages.

Starters

Dependency bundles such as spring-boot-starter-web or spring-boot-starter-data-jpa that bring in compatible libraries, which in turn activate their auto-configuration.

Auto-configuration classes

Listed in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each is a @AutoConfiguration class guarded by conditions.

Back off

Most auto-configured beans use @ConditionalOnMissingBean, so defining your own bean of that type replaces the default.

Step by step

What happens at startup

  • SpringApplication.run creates the ApplicationContext and reads properties and profiles
  • Component scanning registers your beans
  • Auto-configuration classes are evaluated in order; conditions decide which apply
  • Beans are created; the embedded server (Tomcat by default) starts
  • ApplicationReadyEvent fires

Examples

Writing your own auto-configuration

@AutoConfiguration
@ConditionalOnClass(SmsClient.class)
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfiguration {
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "sms", name = "enabled", havingValue = "true", matchIfMissing = true)
    SmsClient smsClient(SmsProperties props) {
        return new SmsClient(props.apiKey(), props.sender());
    }
}

# see the conditions report
java -jar app.jar --debug

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Exclude DataSourceAutoConfiguration and explain when you would.
  • Replace the auto-configured ObjectMapper with your own and confirm the default backs off.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

How do you disable a specific auto-configuration?

With @SpringBootApplication(exclude = DataSourceAutoConfiguration.class), or the property spring.autoconfigure.exclude. Often defining your own bean is enough because the default backs off.

Spring vs Spring Boot?

Spring Framework provides IoC, MVC, data access, transactions and more. Spring Boot is opinionated setup on top: starters, auto-configuration, embedded servers, externalised configuration and Actuator, so you can run a production-ready app with little configuration.

Summary

@SpringBootApplication enables scanning and auto-configuration; starters bring libraries whose conditional auto-configurations create default beans that back off when you define your own. Use --debug to see what applied.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus