DEV Community

Cover image for Spring Boot Auto-Configuration: How It Works with Examples
Rachit Joshi
Rachit Joshi

Posted on

Spring Boot Auto-Configuration: How It Works with Examples

INTRODUCTION

Spring Boot is popular because it reduces the amount of configuration developers need to write. One of its most useful features is auto-configuration, which automatically configures your application based on the dependencies available in the project.

In this article, we’ll understand what Spring Boot auto-configuration is, how it works, and how to customize it when needed.

What Is Spring Boot Auto-Configuration?

Spring Boot auto-configuration automatically configures a Spring application based on the JAR dependencies added to the project.

For example, when you add the H2 database dependency and have not manually configured a database connection, Spring Boot can automatically configure an in-memory database.

This lets developers focus on application logic and spend less time writing repetitive configuration.

How Does Auto-Configuration Work?

Spring Boot checks the dependencies available in the classpath and applies configuration when certain conditions are satisfied.

The process can be understood as:

1. Add dependencies

You add a starter such as spring-boot-starter-web or spring-boot-starter-data-jpa.

2. Spring Boot checks conditions

It checks whether relevant classes, properties, and existing beans are available.

3. Configuration is applied

Spring Boot creates the required beans and applies suitable default settings.

4. Your application starts

You can use the configured features without writing every configuration class manually.

Enabling Auto-Configuration

Auto-configuration is enabled through the @EnableAutoConfiguration annotation. In most Spring Boot applications, you do not need to add it separately because it is already included in @SpringBootApplication.

The @SpringBootApplication annotation combines:

@Configuration
@EnableAutoConfiguration
@ComponentScan

Therefore, a typical Spring Boot application can simply use:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}
Enter fullscreen mode Exit fullscreen mode

}

Here, @SpringBootApplication enables configuration, component scanning, and auto-configuration together.

Example: Spring Boot Web Application

Suppose you create a project using Spring Initializr and add the Spring Web dependency.

Without Spring Boot auto-configuration, you would need to configure several web-related components manually.

With auto-configuration, Spring Boot can configure the web infrastructure automatically.

For example:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("/")
public String home() {
    return "Hello, Spring Boot!";
}
Enter fullscreen mode Exit fullscreen mode

}

After starting the application, Spring Boot handles the required web setup, allowing the controller to respond to requests.

Example: Database Auto-Configuration

Suppose you add the H2 database dependency to your project.

If no custom database configuration is provided, Spring Boot can configure an in-memory database automatically.

A typical Maven dependency looks like this:


com.h2database
h2
runtime

You can then configure application properties if needed:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=true

Spring Boot uses these properties and the available dependencies to configure the database connection.

What Is spring-boot-autoconfigure.jar?

The auto-configuration logic provided by Spring Boot is contained in the spring-boot-autoconfigure module.

It includes many auto-configuration classes that configure features such as:

Web applications

Databases

JPA and Hibernate

Security

Messaging

Caching

Validation

These configurations are applied conditionally, depending on the application environment.

Conditional Auto-Configuration

Spring Boot does not blindly configure every feature. It uses conditions to decide whether a configuration should be applied.

Common conditional annotations include:

@ConditionalOnClass

Applies configuration when a particular class is available on the classpath.

@ConditionalOnClass(SomeLibrary.class)
@ConditionalOnMissingBean

Applies configuration when a specific bean has not already been defined.

@bean
@ConditionalOnMissingBean
public SomeService someService() {
return new SomeService();
}

This allows developers to replace default behavior with their own implementation.

@ConditionalOnProperty

Applies configuration when a particular application property matches the required condition.

@ConditionalOnProperty(
name = "feature.enabled",
havingValue = "true"
)

These conditions make auto-configuration flexible and non-invasive.

How to Disable Auto-Configuration

Sometimes, you may not want Spring Boot to configure a particular feature.

You can exclude a specific auto-configuration class using the exclude attribute.

For example:

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

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

public static void main(String[] args) {
    org.springframework.boot.SpringApplication.run(
        DemoApplication.class, args
    );
}
Enter fullscreen mode Exit fullscreen mode

}

You can also exclude auto-configuration using the application properties file:

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

Note: The exact auto-configuration class name can vary between Spring Boot versions. Always check the documentation for the version you are using.

How to See Which Auto-Configurations Are Applied

If you want to understand what Spring Boot is configuring, you can start the application with the --debug option.

java -jar your-application.jar --debug

Spring Boot then displays a conditions evaluation report, which helps you see:

Which auto-configurations matched

Which configurations did not match

Why a particular configuration was applied or skipped

This is especially useful when troubleshooting startup issues.

Advantages of Auto-Configuration

1. Less Boilerplate

You do not need to manually configure every common component.

2. Faster Development

Spring Boot provides sensible defaults, helping you create applications quickly.

3. Easy Customization

You can override default beans or exclude specific auto-configurations when necessary.

4. Dependency-Based Configuration

The features available in your application are influenced by the dependencies you add.

5. Better Productivity

Developers can focus on business logic instead of repetitive infrastructure setup.

Conclusion

Spring Boot auto-configuration is one of the main features that makes Spring Boot easier to use. It automatically configures application components based on the dependencies and conditions available in the project.

By understanding how auto-configuration works, developers can build applications faster while still having the flexibility to customize or disable default behavior when required.

Top comments (0)