How will enable and disable auto configuration in spring boot?

Auto-configuration in Spring Boot can be selectively disabled using the exclude attribute on @EnableAutoConfiguration or @SpringBootApplication, or fully turned off with the spring.autoconfigure.exclude property.

Key Points: • @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) disables one specific auto-configuration class at compile time. • spring.autoconfigure.exclude in application.properties achieves the same thing without touching code, useful for environment-specific overrides. • Auto-configuration itself is never fully "disabled" globally in a supported way -- you exclude specific classes, not the mechanism as a whole. • Excluding a needed auto-configuration without providing a replacement bean will cause startup failures for anything that depended on it. • Conditional annotations already prevent unnecessary auto-configuration from activating, so explicit exclusion is mainly needed when you want full manual control over a specific concern.

Example: A team that manages its own DataSource bean entirely by hand might exclude DataSourceAutoConfiguration to avoid any conflict or duplicate bean definition, while leaving all other auto-configuration active.

Code Example:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApp { }

# or, via properties:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Interview Tip: A concise interview answer is:

"To disable a specific auto-configuration, I use the exclude attribute on @SpringBootApplication, like excluding DataSourceAutoConfiguration.class, or set spring.autoconfigure.exclude in properties for the same effect without a code change. There's no single switch to turn off all auto-configuration -- you exclude individual classes as needed."