Hook: Interviewers love this one because a single exclusion can save startup time—or cause a missing-bean crash if you remove the wrong auto-config.
Question: How do you disable Auto Configuration?
Answer: In Spring Boot, you usually disable specific auto-configurations, not all of them. The common ways are `@SpringBootApplication(exclude = ...)`, `@EnableAutoConfiguration(exclude = ...)`, or the `spring.autoconfigure.exclude` property with fully qualified class names. If you truly want no auto-configuration at all, you use plain `@Configuration` and `@ComponentScan` instead of Boot’s auto-config annotations.
Interview-Ready Answer: I disable auto-configuration by excluding the auto-config classes I do not want. I can do that directly on `@SpringBootApplication`, on `@EnableAutoConfiguration`, or externally with `spring.autoconfigure.exclude` in properties or the environment. If I want to turn off auto-configuration completely, I do not use `@EnableAutoConfiguration` at all and fall back to plain Spring configuration. The important detail is that Spring Boot filters those classes before it imports them, so the unwanted beans are never created.
Spring Boot auto-configuration is the part that adds sensible default beans based on the classpath and existing beans. It is not the same as component scanning. When you disable one auto-config class, Boot simply stops offering that default setup; your own `@Bean` methods and scanned components still work.
| Method | Best for | Notes |
|---|---|---|
| `exclude` | Known classes | Compile-time safe |
| `excludeName` | Optional deps | String-based |
| `spring.autoconfigure.exclude` | Env-specific | No code change |
| No auto-config annotation | Full control | Plain Spring app |
Use exclusion when Boot guessed wrong: for example, JDBC starter on the classpath but no database, a scheduler you do not want, or security defaults that conflict with a custom setup. Do not use it just to make startup quieter; if a bean is there for a good reason, replacing it with your own bean is often cleaner than removing Boot’s default.
The filter step is basically linear in the number of candidate auto-configurations, so think O(n) where n is usually a few dozen to a couple hundred classes depending on starters. The real savings come from skipping the beans themselves: avoiding database connection setup, ORM initialization, schema checks, or embedded server wiring can save hundreds of milliseconds and sometimes seconds. In Spring Boot 3, discovery moved to the `AutoConfiguration.imports` file; the exclusion API stayed the same, so the interview answer is stable across versions.
Excluding an auto-config class does not stop your own manual `@Bean` methods from running. Also, excluding by class name only works if the string is exact; a typo silently leaves the unwanted auto-config in place. Finally, remember that some auto-configurations depend on others, so removing one may cause a downstream auto-config to back off or fail because a required bean is gone.
Real-world story: A checkout service in an e-commerce system added `spring-boot-starter-jdbc` only because a shared library pulled it in. On the next deploy, Boot tried to auto-configure a datasource, but the service had no `spring.datasource.url`, so startup failed with `Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured`. Users saw 503s because the pod never became ready. The fix was either to remove the unwanted starter or exclude `DataSourceAutoConfiguration` until the service actually needed a database. The lesson: a single auto-configured bean can decide whether your app starts or stalls at the door.
package com.example.autoconfigdemo;
import java.util.Map;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.task.TaskExecutor;
@SpringBootApplication
public class AutoConfigDemoApplication {
public static void main(String[] args) {
// First run: let Boot auto-configure as usual.
runScenario("Normal startup", Map.of());
// Second run: tell Boot not to import TaskExecutionAutoConfiguration.
// This is the same idea as using @SpringBootApplication(exclude = ...),
// but shown here through externalized configuration.
runScenario("Excluded TaskExecutionAutoConfiguration", Map.of(
"spring.autoconfigure.exclude",
TaskExecutionAutoConfiguration.class.getName()
));
}
private static void runScenario(String label, Map<String, Object> properties) {
System.out.println("\n=== " + label + " ===");
try (ConfigurableApplicationContext context = new SpringApplicationBuilder(AutoConfigDemoApplication.class)
.web(WebApplicationType.NONE)
.properties(properties)
.run()) {
// If auto-configuration is active, Boot contributes the applicationTaskExecutor bean.
boolean hasBean = context.containsBean("applicationTaskExecutor");
System.out.println("containsBean('applicationTaskExecutor') = " + hasBean);
try {
TaskExecutor executor = context.getBean(TaskExecutor.class);
System.out.println("TaskExecutor bean type = " + executor.getClass().getName());
} catch (NoSuchBeanDefinitionException ex) {
// This is the failure path you'd see in real code if something expects
// an auto-configured bean that you excluded.
System.out.println("Lookup failed: " + ex.getMessage());
}
}
}
}
Follow-up & Tricky Questions:
Common Mistakes:
Memory Hook: Think of Spring Boot as a buffet: exclusions put a small ‘do not serve’ sign on one dish, while removing `@EnableAutoConfiguration` closes the whole kitchen.
Cheat Sheet:
Practice Tasks: