Spring Boot Auto Configuration and Dispatcher Servlet
In this article, I am going to discuss Spring Boot Auto Configuration and Dispatcher Servlet with Examples. Please read our previous article where we discussed Spring Boot EhCache with Examples.
Spring Boot Auto Configuration
Spring Boot is a framework that simplifies the development of Spring applications by providing default configurations and reducing the amount of boilerplate code required.
It automatically configures an application based on the dependencies present in the classpath, such as jars, beans, and properties files. This makes development easier and faster because there is no need to define certain beans included in the auto-configuration classes.
A typical MVC database-driven Spring application requires many configurations, such as a dispatcher servlet, view resolver, Jackson, data source, and transaction manager. With Spring Boot’s auto-configuration feature, these configurations are automatically set up based on the dependencies present in the classpath.
Auto-configuration can be enabled by adding the @SpringBootApplication or @EnableAutoConfiguration annotation to the start-up class (the class containing the main() function). This indicates that the file is a Spring context file and enables auto-configuration and component scanning. Component scanning is a feature of Spring that automatically scans classes in the package and any sub-packages for beans. This allows for easy discovery and configuration of components within the application.
Overall, Spring Boot’s auto-configuration feature simplifies the development process by reducing the amount of manual configuration required and allowing developers to focus on building their application’s functionality.
Exclude classes from auto-configuration
Why do we need to exclude classes from autoconfiguration?
Excluding dependencies from auto-configuration can improve the start-up speed and performance of the application. This is because auto-configuration uses a default configuration mechanism. By manually configuring a dependency, its overall load on the system can be reduced, making it more efficient.
How to exclude classes from autoconfiguration?
Classes can be excluded from auto-configuration by adding the following line alongside the @SpringBootApplication annotation:
Alternatively, the application.properties file can be modified to exclude autoconfiguration:
We can see the auto-configuration done by Spring Boot in the auto-configuration report or conditions evaluation report. The autoconfiguration report can be generated by enabling debug mode. Open the application.properties file and add the following statement:
The autoconfiguration information will be printed in the terminal, alongside the other terminal output.
In Spring MVC all incoming requests go through a single servlet, which is known as the Dispatcher Servlet. The Dispatcher Servlet is a design pattern in web application development. A single servlet receives all the requests and transfers them to all other components of the application. This paradigm can be described using the following diagram:
The Dispatcher Servlet is responsible for handling incoming Uniform Resource Identifiers (URIs) and finding the appropriate combination of handlers (also known as controller classes) and views (usually JSPs). Once the view is determined, the Dispatcher Servlet renders it as the response and returns the Response Object to the client.
In the next article, I am going to discuss Spring Boot HATEOAS with Examples. Here, in this article, I try to explain Spring Boot Auto Configuration and Dispatcher Servlet with Examples. I hope you enjoy this Spring Boot Auto Configuration and Dispatcher Servlet article.