Can you customize a specific auto-configuration in Spring Boot?

Yes, Spring Boot allows developers to customize or override specific auto-configurations. While Spring Boot automatically configures components based on dependencies and application settings, it also provides several ways to modify this behavior to meet application-specific requirements.

Key Points

What is Auto-Configuration?

Spring Boot automatically configures common components such as

Example

If Spring Boot detects

spring-boot-starter-data-jpa

and a database driver,

it automatically creates

without requiring manual configuration.

How Can We Customize Auto-Configuration?

1. Using application.properties or application.yml

Many auto-configured settings can be overridden through configuration files.

Example

spring.datasource.url=jdbc:mysql://localhost:3306/testdb

spring.datasource.username=root

spring.datasource.password=admin

Spring Boot uses these values instead of its defaults.

2. Defining a Custom Bean

If we define our own bean, Spring Boot usually backs off and uses our implementation.

@Configuration

public class AppConfig {

    @Bean
    public DataSource dataSource() {

        return new HikariDataSource();
    }
}

Here, Spring Boot uses the custom DataSource bean instead of creating one automatically.

3. Excluding Auto-Configuration

Sometimes an auto-configuration is not needed.

Example

@SpringBootApplication(
    exclude = {
        DataSourceAutoConfiguration.class
    }
)

public class Application {

}

This prevents Spring Boot from configuring a DataSource.

4. Using @Conditional Annotations

Custom configurations can be loaded only when certain conditions are met.

@Configuration

@ConditionalOnProperty(
        name = "feature.enabled",
        havingValue = "true")
public class FeatureConfig {

}

This configuration becomes active only when

feature.enabled=true

5. Using @Primary

When multiple beans of the same type exist, one can be marked as the preferred bean.

@Bean

@Primary
public DataSource customDataSource() {

    return new HikariDataSource();
}

Spring will inject this bean by default.

Example

Suppose Spring Boot automatically configures a DataSource for MySQL.

Application Requirement

Use a custom connection pool configuration.

Solution

This allows customization without disabling the entire auto-configuration mechanism.

Common Auto-Configurations That Are Customized

Benefits of Customizing Auto-Configuration

Real-World Example

In an enterprise application

Spring Boot automatically creates

HikariCP DataSource

Business Requirement

Instead of disabling Spring Boot completely

This keeps the benefits of auto-configuration while allowing fine-grained customization.

Interview Tip

A concise interview answer is

Yes, Spring Boot auto-configuration can be customized in several ways. We can override default settings using application.properties or application.yml, define our own beans to replace auto-configured beans, exclude specific auto-configurations using the exclude attribute, or use conditional annotations for custom behavior. This flexibility allows us to leverage Spring Boot's automation while still meeting application-specific requirements.

Last updated: 11 November 2026

Previous question: Why is auto-configuration important in a Spring Boot application?

Next question: How do starters simplify the Maven or Gradle configuration?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions