Hook: Spring Boot auto configuration is like a smart hotel room: it quietly sets up the bed, lights, and Wi-Fi before you arrive, but it steps aside if you bring your own furniture.
Question: What is Auto Configuration?
Answer: Auto configuration is a Spring Boot feature that creates and wires beans for you based on what libraries are on the classpath (the jars available when the app starts), what properties you set, and what beans already exist. A condition is a rule that says create this bean only if a check passes, such as a class being present or a property being enabled. This gives you a working app with sensible defaults and far less manual setup.
Interview-Ready Answer: I’d say Spring Boot auto configuration is the feature that sets up common beans automatically based on the libraries I included, the properties I configured, and the beans I already defined. It is enabled by @EnableAutoConfiguration through @SpringBootApplication, and it uses conditions like @ConditionalOnClass and @ConditionalOnMissingBean so Boot can back off when I provide a custom bean. In practice, that is why adding a starter can instantly wire things like MVC, Jackson, Tomcat, or a datasource with very little code.
Auto configuration is Spring Boot’s way of saying: if I see the right ingredients, I will build the standard beans for you. A starter is just a dependency bundle, like spring-boot-starter-web or spring-boot-starter-data-jpa, that brings the common libraries together so Boot can make good guesses. The big idea is convention over configuration: you get a useful default first, then override only what is special in your app.
@SpringBootApplication, which includes @EnableAutoConfiguration.META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports; older Boot 2 projects often used spring.factories.AutoConfigurationImportSelector, selects which auto-config classes should be considered.@ConditionalOnClass (is a library present?), @ConditionalOnProperty (did the user enable it?), @ConditionalOnBean (does another bean exist?), and @ConditionalOnMissingBean (should Boot back off because the user already provided one?).Use auto configuration when you want standard infrastructure to work with almost no setup: web servers, JSON mapping, validation, data access, messaging, or security defaults. It is perfect for fast startup, consistent team conventions, and fewer boilerplate files. Do not fight it for unusual edge cases; instead, override only the specific bean or property you need.
| Approach | Who creates beans | Best for | Trade-off |
|---|---|---|---|
| Auto configuration | Spring Boot | Common defaults | Less explicit |
Manual @Bean | You | Special cases | More code |
| Component scanning | Spring container | Your own services | Not for infra defaults |
For interview purposes, think of the cost as roughly O(n) over candidate auto-config classes and their conditions, where n is the number of possible configurations on the classpath. In practice, the cost is small; the bigger startup time usually comes from bean creation, database pools, entity scanning, or server startup, not from checking conditions. Space use is modest because Boot stores bean definitions and condition results rather than building every possible branch.
One useful version detail: Boot 3 uses @AutoConfiguration and the AutoConfiguration.imports file, while older Boot 2 code often relied on spring.factories. Another important edge case is backoff: if you define your own bean of the same type, Boot often skips its default one on purpose. You can also disable specific auto-configurations with @SpringBootApplication(exclude = ...) or spring.autoconfigure.exclude.
Real-World Example: Imagine a checkout service for an online store. The team adds spring-boot-starter-web and spring-boot-starter-data-jpa, and Boot automatically wires Tomcat, Jackson, transaction management, and a datasource. That means the service can focus on business code while Boot handles the standard plumbing.
Now a developer accidentally adds H2 as a runtime dependency instead of test-only. Auto configuration sees H2 on the classpath and may choose an embedded database path if the production datasource settings are missing or wrong. The app starts, tests pass, but real orders disappear after a restart because they were written to memory, not to Postgres.
The symptoms are easy to miss at first: logs mention an embedded datasource, metrics show no real database pool, and support sees customers receive order IDs that later cannot be found. The outage is not caused by auto configuration itself; it is caused by misunderstanding which ingredients Boot is reading from the classpath and properties.
package com.example.demo;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class AutoConfigurationDemoApplication {
public static void main(String[] args) {
SpringApplication.run(AutoConfigurationDemoApplication.class, args);
}
@Bean
ApplicationRunner runner(ObjectProvider<GreetingService> greetingServiceProvider) {
return args -> {
GreetingService service = greetingServiceProvider.getIfAvailable();
// Edge case: if auto-configuration is turned off, the app still starts.
// That is safer than injecting the bean directly and crashing at startup.
if (service == null) {
System.out.println("No GreetingService bean found. Start with --app.greeting.enabled=true.");
return;
}
System.out.println(service.greet("Alex"));
};
}
// If you add a second GreetingService bean here, the auto-configured bean will back off
// because the default is guarded by @ConditionalOnMissingBean.
}
interface GreetingService {
String greet(String name);
}
@Configuration(proxyBeanMethods = false)
class GreetingAutoConfiguration {
@Bean
@ConditionalOnMissingBean(GreetingService.class)
@ConditionalOnProperty(prefix = "app.greeting", name = "enabled", havingValue = "true", matchIfMissing = true)
GreetingService greetingService() {
return name -> "Hello, " + name + " from Spring Boot auto-configuration!";
}
}
@AutoConfiguration class or a @Configuration class with conditional beans, then register it in the auto-configuration imports file. Use @ConditionalOnClass and @ConditionalOnMissingBean so your library behaves politely.@SpringBootApplication(exclude = ...) for a specific class, or spring.autoconfigure.exclude in properties. That is useful when Boot’s default is close, but not right for your environment.@ConditionalOnMissingBean, which prevents duplicate infrastructure and gives your custom bean priority.@SpringBootApplication only mean component scanning? No. It combines component scanning, configuration, and auto configuration. That is why one annotation feels so powerful.Common Mistakes:
Memory Hook: Think of Boot as a smart hotel manager: if the room is empty, it sets up the standard furniture; if you bring your own sofa, it leaves space and does not double-park one.
Cheat Sheet:
@SpringBootApplication includes @EnableAutoConfiguration.@ConditionalOnMissingBean is the main backoff rule.AutoConfiguration.imports; Boot 2 often used spring.factories.Practice Tasks:
spring-boot-starter-web and confirm that Tomcat starts without any manual server bean.ObjectMapper or datasource bean and observe how Boot backs off from its default.--app.greeting.enabled=false and see the graceful missing-bean path.