How to disable a specific auto-configuration class?

Spring Boot allows developers to disable specific auto-configuration classes when the default configuration is not required or when custom configurations need to be used. This can be achieved through annotations or application properties, giving full control over how the application is configured.

Key Points: • Specific auto-configuration classes can be excluded from Spring Boot's startup process. • Disabling unnecessary auto-configurations can improve startup performance and avoid unwanted bean creation. • Auto-configurations can be excluded using annotations or configuration properties. • Commonly excluded configurations include database, security, and messaging auto-configurations. • Excluding auto-configuration is useful when custom implementations are provided.

Why Would We Disable Auto-Configuration?

Spring Boot automatically configures components based on available dependencies.

However, there are situations where:

• A custom configuration is required. • Certain features are not being used. • Auto-configured beans cause conflicts. • Startup optimization is needed.

In such cases, disabling specific auto-configurations is beneficial.

Method 1: Using @SpringBootApplication

The most common approach is using the exclude attribute.

Code Example:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

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

    public static void main(String[] args) {

        SpringApplication.run(
                Application.class,
                args);
    }
}

In this example:

• Spring Boot will not create a DataSource automatically. • Useful when the application does not use a database or uses custom database configuration.

Method 2: Using @EnableAutoConfiguration

Code Example:

@Configuration

@EnableAutoConfiguration(
    exclude = {
        DataSourceAutoConfiguration.class
    }
)
public class AppConfig {

}

This provides the same functionality using the underlying annotation.

Method 3: Using application.properties

Auto-configurations can also be excluded through configuration files.

application.properties

spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Multiple classes can be specified:

spring.autoconfigure.exclude=\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

This approach is useful when configuration changes are environment-specific.

Common Auto-Configurations That Are Frequently Excluded

Database:

DataSourceAutoConfiguration

Security:

SecurityAutoConfiguration

JPA:

HibernateJpaAutoConfiguration

MongoDB:

MongoAutoConfiguration

Redis:

RedisAutoConfiguration

Example: Suppose a Spring Boot application includes:

spring-boot-starter-data-jpa

but the application does not connect to a database yet.

Without exclusion:

Spring Boot attempts to create a DataSource and may fail during startup.

Solution:

@SpringBootApplication(
    exclude = {
        DataSourceAutoConfiguration.class
    }
)

The application starts successfully without requiring database configuration.

Benefits of Disabling Auto-Configuration

• Prevents unnecessary bean creation • Avoids startup failures • Allows custom implementations • Improves application flexibility • Simplifies troubleshooting

Real-World Example

Consider a microservice responsible only for processing messages from a queue.

Dependencies include:

• Spring Web • Spring Security • Spring Data JPA

Since the service does not use a database:

DataSourceAutoConfiguration

can be excluded to avoid unnecessary configuration and startup overhead.

Interview Tip: A concise interview answer is:

"We can disable a specific auto-configuration in Spring Boot using the exclude attribute of @SpringBootApplication or @EnableAutoConfiguration. Alternatively, we can use the spring.autoconfigure.exclude property in application.properties or application.yml. This is useful when we want to prevent Spring Boot from creating default beans and instead use custom configurations."