Spring Boot AutoConfiguration is a feature of the Spring Boot framework that automatically configures and sets up certain beans in the application context based on the dependencies present in the classpath.
This helps developers to quickly set up and bootstrap their applications without having to manually define and configure each bean.
To understand how Spring Boot AutoConfiguration works, it is important to first understand the concept of bean auto-wiring in Spring.
In Spring, a bean is a Java object that is managed by the Spring container. The Spring container is responsible for creating and initializing beans, as well as injecting their dependencies. Bean auto-wiring is a feature of the Spring framework that allows the container to automatically inject the dependencies of a bean by matching the bean’s properties with the available beans in the application context.
This is done by matching the bean’s type, name, or qualifier.
Spring Boot AutoConfiguration
takes this concept of bean auto-wiring one step further by automatically configuring and setting up certain beans in the application context based on the dependencies present in the classpath.
This is done using the @EnableAutoConfiguration
annotation, which is typically added to the main class of the Spring Boot application.

EnableAutoConfiguration
exampleAutoConfiguration Example
Here is an example of how Spring Boot AutoConfiguration might be used in a Spring Boot application:
Imagine that you are building a web application that uses the Hibernate library for object-relational mapping (ORM). In order to use Hibernate, you need to set up a Hibernate SessionFactory bean in your application context.
With Spring Boot AutoConfiguration, you can simply include the Hibernate library as a dependency in your project, and Spring Boot will automatically set up and configure a Hibernate SessionFactory bean in the application context for you.
Here is an example of how you might include the Hibernate dependency in your project:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.1.6.Final</version>
</dependency>
With this dependency in place, Spring Boot will automatically set up and configure a Hibernate SessionFactory bean in the application context when the application is started. You can then use the Hibernate SessionFactory bean in your application code to interact with the database.
This is just one example of how Spring Boot AutoConfiguration can be used to simplify the process of setting up and configuring dependencies in a Spring Boot application. There are many other dependencies that can be automatically configured using Spring Boot AutoConfiguration, such as template engines, data source configurations, and more.
How AutoConfiguration works internally in Spring Boot?
Auto-configuration in Spring Boot works by scanning the classpath for certain classes or packages that are annotated with special markers.
For example, if Spring Boot finds a class or package that is annotated with @EnableAutoConfiguration
, it will automatically attempt to configure the beans required for that class or package.
Similarly, if it finds a class or package that is annotated with @ConditionalOnClass
, it will only attempt to configure the beans required for that class or package if the specified class is present on the classpath.
The auto-configuration process in Spring Boot is driven by a special file called spring.factories
, which is located in the META-INF
directory of the classpath.
This file contains a list of @EnableAutoConfiguration
annotated classes that should be used to configure the application. Spring Boot uses the SpringFactoriesLoader
class to read this file and determine which @EnableAutoConfiguration
annotated classes should be used.
For example, consider the following spring.factories
file:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
In addition to the built-in auto-configuration features of Spring Boot, you can also customize the auto-configuration process by writing your own @Conditional
annotations or by excluding certain auto-configured beans using the spring.autoconfigure.exclude
property in your application.properties file.
This allows you to tailor the auto-configuration process to your specific needs and ensure that only the beans and dependencies that you need are set up for your application.
Overall, the auto-configuration feature in Spring Boot is a powerful tool that can save you a lot of time and effort when building and running Spring applications.
By leveraging the power of auto-configuration, you can focus on building your application’s core functionality, rather than worrying about the details of bean and dependency configuration.
How to disable AutoConfiguration in Spring Boot?
There may be times when you want to disable AutoConfiguration for certain components.
To disable AutoConfiguration in Spring Boot, you can use the spring.autoconfigure.exclude property in your application.properties or application.yml file.
This property takes a comma-separated list of fully qualified class names, and any AutoConfiguration classes with those names will be excluded from the application.
For example, suppose you have a Spring Boot application that uses Hibernate as its JPA vendor and you want to disable the HibernateJpaAutoConfiguration
class, which is responsible for configuring Hibernate as the JPA provider. To do this, you would add the following line to your application.properties
file:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
Alternatively, you can use the @EnableAutoConfiguration
annotation and specify the classes that should be excluded from AutoConfiguration
. This can be done by passing an array of classes to the exclude attribute of the annotation. For example:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyApplication {
...
}
This will disable the DataSourceAutoConfiguration
class, which is responsible for configuring a DataSource
bean for use with JDBC.
It’s important to note that while disabling AutoConfiguration
can be useful in certain situations, it is generally recommended to use AutoConfiguration
whenever possible.
This is because AutoConfiguration
can greatly simplify the configuration of your application and help you get up and running more quickly.
On the other hand, if you choose to disable AutoConfiguration
for certain components, you will need to manually configure those components yourself.
This can be time-consuming and error-prone, and may require a deeper understanding of the inner workings of your application.
Therefore, it is usually best to use AutoConfiguration
whenever possible and only disable it when absolutely necessary.
How to display Auto-Configuration Report in Spring Boot
To debug auto-configuration in Spring Boot, you can:
- Use the
--debug
flag when starting the application to enable debug logging for the auto-configuration process. This will output detailed information about the configuration process to the console, including which auto-configuration classes are being applied and why. - Set the
debug
property totrue
using theSpringApplication.setDefaultProperties
method before the application context is created. This will enable debug logging for the auto-configuration process in the same way as the--debug
flag. - Exclude specific auto-configuration classes from being applied using the
spring.autoconfigure.exclude
property. This can be helpful if you suspect that a specific auto-configuration class is causing problems. - Override the default auto-configuration provided by Spring Boot using the
spring.factories
file. This can be helpful if you want to customize the auto-configuration process or provide your own auto-configuration classes. - Get a report of the auto-configuration that was applied to the application context using the
SpringApplication.getAutoConfigurationReport()
method. This can be helpful for understanding why certain beans were created or why certain properties were set.
Here is an example of a sample auto-configuration report output in Spring Boot. This report shows the auto-configuration classes that were applied to the application context.
Positive matches
Positive matches:
-----------------
DataSourceAutoConfiguration matched:
- @ConditionalOnClass found required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition)
- @ConditionalOnMissingBean (types: org.springframework.boot.jdbc.DataSourceInitializer; SearchStrategy: all) did not find any beans (OnBeanCondition)
- DataSourceUrlMetaDataConditions matched:
- @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) found no beans (OnBeanCondition)
- @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (OnPropertyCondition)
- DataSourcePoolMetadataConditions matched:
- @ConditionalOnSingleCandidate (types: javax.sql.DataSource; SearchStrategy: all) found no beans (OnBeanCondition)
- AnyNestedCondition 2 matched 1 did not; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameAvailableCondition.PoolPasswordAvailableCondition.PoolJmxAvailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameAvailableCondition.PoolPasswordAvailableCondition.PoolJmxUnavailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameAvailableCondition.PoolPasswordUnavailableCondition.PoolJmxAvailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameAvailableCondition.PoolPasswordUnavailableCondition.PoolJmxUnavailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameUnavailableCondition.PoolPasswordAvailableCondition.PoolJmxAvailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameUnavailableCondition.PoolPasswordAvailableCondition.PoolJmxUnavailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameUnavailableCondition.PoolPasswordUnavailableCondition.PoolJmxAvailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.PoolXADataSourceAvailableCondition.PoolDriverClassNameAvailableCondition.PoolUrlAvailableCondition.PoolUsernameUnavailableCondition.PoolPasswordUnavailableCondition.PoolJmxUnavailableCondition; NestedCondition on DataSourcePoolMetadataConditions.PoolAvailableCondition.PoolDataSourceAvailableCondition.
The first line, “Positive matches”, indicates that the following auto-configuration classes were successfully applied.
The next line, “DataSourceAutoConfiguration matched,” indicates that the DataSourceAutoConfiguration
class was applied to the application context. This class is responsible for setting up a connection to a database using the DataSource
interface.
The lines following this show the conditions that were met in order for the DataSourceAutoConfiguration
class to be applied. The @ConditionalOnClass
annotation checks for the presence of a required class, in this case org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
. The @ConditionalOnMissingBean
annotation checks for the absence of a specific bean, in this case org.springframework.boot.jdbc.DataSourceInitializer
. The DataSourceUrlMetaDataConditions
and DataSourcePoolMetadataConditions
classes check for the presence or absence of certain properties and beans.
The final lines show a nested condition that was checked in order to determine whether the DataSourcePoolMetadataConditions
class should be applied. This nested condition checks for the presence or absence of certain properties and beans related to pooling the database connection.
Negative matches
Here is an example of a sample auto-configuration report output in Spring Boot showing negative matches:
Negative matches:
-----------------
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)
AopAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.aspectj.lang.Aspects' (OnClassCondition)
DataSourceAutoConfiguration.JdbcTemplateConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.springframework.jdbc.core.JdbcTemplate' (OnClassCondition)
DataSourceTransactionManagerAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.DataSourceTransactionManager' (OnClassCondition)
JpaBaseConfiguration.JpaWebConfiguration:
Did not match:
- @ConditionalOnWebApplication (required) found WebApplicationType [NONE] (OnWebApplicationCondition)
JpaRepositoriesAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.springframework.data.jpa.repository.JpaRepository' (OnClassCondition)
JtaAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager' (OnClassCondition)
This output shows the auto-configuration classes that were not applied to the application context because the conditions for their application were not met. For example, the ActiveMQAutoConfiguration
class was not applied because the required class org.apache.activemq.ActiveMQConnectionFactory
was not present on the classpath. The DataSourceAutoConfiguration.JdbcTemplateConfiguration
class was not applied because the required class org.springframework.jdbc.core.JdbcTemplate
was not present on the classpath. The JpaBaseConfiguration.JpaWebConfiguration
class was not applied because the application is not a web application.
Spring Boot Actuator to debug auto-configuration
Spring Boot Actuator is a set of tools that can be used to monitor and manage a Spring Boot application. One of the features of Actuator is the ability to view the auto-configuration report for an application.
To use Actuator to debug auto-configuration in a Spring Boot application, you will need to include the spring-boot-starter-actuator
dependency in your project and enable the auto-configuration report endpoint.
Here is an example of how to do this in a Maven project:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
To enable the auto-configuration report endpoint, you can add the following property to your application.properties
file:
management.endpoints.web.exposure.include=autoconfig
Once the Actuator dependency and the auto-configuration report endpoint are enabled, you can access the auto-configuration report by making a GET request to the /actuator/autoconfig
endpoint. For example, if your application is running on localhost
on port 8080
, you can access the auto-configuration report at the following URL: http://localhost:8080/actuator/autoconfig
.
You can also use the SpringBootAdmin
application to view the auto-configuration report for your application. SpringBootAdmin is a third-party application that provides a user-friendly interface for viewing Actuator metrics and information about your application.