Table of contents
1.
Introduction
2.
What is Spring Boot Auto-configuration?
3.
How to Use Spring Boot Auto-configuration?
4.
Replacing auto-configuration
5.
Disabling Specific Auto-configuration
6.
Debugging Auto-configuration
7.
Frequently Asked Questions
7.1.
What is a Spring Boot?
7.2.
What is Spring Boot Auto-configuration?
7.3.
How does Spring Boot's auto-configuration help?
8.
Conclusion
Last Updated: Oct 24, 2024

Spring Boot Auto-configuration

Author Juhi Sinha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Spring Boot is an essential part of the Spring ecosystem. The presence of Auto-configuration ability in Spring Boot has made the life of developers easier.

In this article, we will learn Spring Boot Auto-configuration. You must have a basic understanding of Spring Boot. Also, you can enroll in our Spring Boot Course if you want to learn more about spring boot. So, without any further ado, let's get started!

What is Spring Boot Auto-configuration?

Spring Boot auto-configuration assists us in automatically configuring our Spring Boot application. For example, if Spring Boot finds HSQLDB in our classpath, it will create an in-memory Database for us. Consider auto-Configuration to be an intelligent system that can provide us with a ready-to-use application based on the jars configured in our classpath. Outside of Spring Boot, it isn't applicable.

How to Use Spring Boot Auto-configuration?

The annotation @EnableAutoConfiguration can be used to enable the auto-configuration feature. However, this annotation isn’t used because it is wrapped inside the @SpringBootApplication annotation.

 

The @ComponentScan@EnableAutoConfiguration, and @Configuration are the three annotations that make up @SpringBootApplication. Instead of using @EnableAutoConfiguration, we use the @SpringBootApplication annotation.

 

Spring Boot auto-configuration looks for Spring MVC on the classpath when we add the spring-boot-starter-web dependency to the project. It sets up a dispatcher servlet, a default error page, and web jars automatically.

When we add the spring-boot-starter-data-jpa dependency, we see that Spring Boot Auto-configuration automatically creates a data source and an Entity Manager.

Replacing auto-configuration

Auto-configuration is non-invasive; we can begin defining our own configuration at any time to replace specific parts of the auto-configuration. If we add our own DataSource bean, the default embedded database support will be removed.

Start the application with the --debug switch to see what auto-configuration is currently being applied and why. This will enable debug logging for several core loggers and generate an auto-configuration report for the console.

Disabling Specific Auto-configuration

If we don't want specific auto-configure classes to be applied, we can disable them with the exclude attribute of @EnableAutoConfiguration.

The code for this looks like:

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;
@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}
You can also try this code with Online Python Compiler
Run Code

 

If the class isn't on the classpath, we can specify the fully qualified name instead of using the annotation's excludeName attribute. Finally, the spring.autoconfigure.exclude property allows us to control the list of auto-configuration classes to exclude.

Debugging Auto-configuration

Using the following two methods, we can learn more about auto-configuration:

  • Debug logging is enabled.
  • Spring Boot Actuator is used.
  • Debug logging is enabled.

By adding a property to the application.properties file, we can debug logging:

logging.level.org.springframework: DEBUG  

Restart the program now. In the log, we can see an auto-configuration report. All auto-configured classes are included in the report. It's split into two sections: positive and negative matches.

We can also use Spring Boot Actuator in the project to debug auto-configuration. To make things easier, we can also include HAL Browser. It displays the details of all auto-configured and non-auto-configured beans.

Frequently Asked Questions

What is a Spring Boot?

Spring Boot is a Spring module that extends the Spring framework's RAD (Rapid Application Development) capabilities.
It's used to create stand-alone spring-based applications that we can just run because spring configuration is minimal.

What is Spring Boot Auto-configuration?

Spring Boot auto-configuration tries to configure our Spring application automatically based on the jar dependencies we've added.

How does Spring Boot's auto-configuration help?

Spring Boot's auto-configuration feature assists in loading default configurations based on the project you're working on. You'll be able to avoid any unnecessary WAR files this way.

Conclusion

In this article, we've learned about Spring Boot Auto-configuration, how to use it, disabling specific Auto-configuration, and debugging in detail.

If you have not gone through the Spring Boot article, you can check it once.

Thanks for reading!

Live masterclass