Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Spring Boot Auto-configuration

Author Juhi Sinha
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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. 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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 {
}

 

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.

FAQs

  1. 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.
     
  2. 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.
     
  3. 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.

Key Takeaways

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. You can also consider our Spring Boot Course to give your career an edge over others.

 

Thanks for reading!

 

Previous article
Spring Boot Packaging
Next article
Spring Boot Hello World Example
Live masterclass