AutoConfiguration in Spring Boot with Example

Spring Boot
What is AutoConfiguration in Spring Boot ?

Autoconfiguration in Spring Boot is a powerful feature that allows developers to easily and quickly configure their Spring applications without having to write boilerplate code.

In other words, Spring Boot’s autoconfiguration feature automatically configures the application based on the dependencies it finds on the classpath.

How to enable AutoConfiguration ?

Enabling autoconfiguration in a Spring Boot application is very simple.

You just need to include the appropriate Spring Boot starter dependencies in your project, and then add the @EnableAutoConfiguration annotation to your main application class.

The @SpringBootApplication annotation is a convenience annotation that combines several other annotations, including @EnableAutoConfiguration.

So, here you don’t need to add @EnableAutoConfiguration explicitly, the @SpringBootApplication will do the work for you.


Here’s an example of how autoconfiguration works in Spring Boot:

Let’s say we want to create a Spring Boot application that uses the Spring Data JPA library to connect to a database. Instead of manually configuring the application and writing all the necessary code, we can simply include the Spring Data JPA dependency in our project’s build file (such as Gradle or Maven), and Spring Boot will automatically configure our application based on that dependency.

For example, we can include the following dependency in our build file:

With this dependency added, Spring Boot will automatically configure the application to use JPA for database access. This includes setting up the required beans, such as the EntityManagerFactory and TransactionManager, as well as configuring default connection settings.

We can then use JPA in our application by simply defining a repository interface and annotating it with the @Repository annotation.

For example:

This repository interface can then be used to perform database operations, such as saving or retrieving users, without any additional configuration required.

How to disable AutoConfiguration ?

In some cases, you may want to disable Spring Boot’s autoconfiguration feature for certain components in your application. You can do this by using the @EnableAutoConfiguration annotation at the class level and specifying the exclude parameter with a list of classes or packages that you want to exclude from autoconfiguration.

Here’s an example:

In this example, we have excluded the DataSourceAutoConfiguration and HibernateJpaAutoConfiguration classes from autoconfiguration, which means that Spring Boot will not attempt to configure a DataSource or Hibernate JPA by default. We can then provide our own configurations for these components.

Alternatively, you can also disable autoconfiguration globally by setting the 'spring.autoconfigure.exclude' property in your or application.yml file.

For example:

This will have the same effect as specifying the @EnableAutoConfiguration annotation with the exclude parameter.


This is just one example of how Spring Boot’s autoconfiguration feature works. By including various starter dependencies, we can easily add functionality to our applications without having to manually configure everything ourselves.

By enabling autoconfiguration in your Spring Boot application, you can save time and effort by letting Spring Boot take care of the configuration for you, while still giving you the flexibility to customize the configuration as needed.

Share This Post On: