Javatpoint Logo
Javatpoint Logo

Spring Boot Auto Configuration and Dispatcher Servlet

In this section, we will see the background functionality of the application:

What is dispatcher servlet?

Who is configuring dispatcher servlet?

What does dispatcher servlet do?

How does the HelloWorldBean object gets converted to JSON?

Who is configuring the error mapping?

Spring Boot Auto Configuration

  • Spring Boot automatically configures a spring application based on dependencies present or not present in the classpath as a jar, beans, properties, etc.
  • It makes development easier and faster as there is no need to define certain beans that are included in the auto-configuration classes.
  • A typical MVC database driven Spring MVC application requires a lot of configuration such as dispatcher servlet, a view resolver, Jackson, data source, transaction manager, among many others.
    • Spring Boot auto-configures a Dispatcher Servlet if Spring MVC jar is on the classpath.
    • Auto-configures the Jackson if Jackson jar is on the classpath.
    • Auto-configures a Data Source if Hibernate jar is on the classpath.
  • Auto-configuration can be enabled by adding @SpringBootApplication or @EnableAutoConfiguration annotation in startup class. It indicates that it is a spring context file.
  • It enables something called auto-configuration.
  • It enable something called components scan. It is the features of Spring where it will start automatically scanning classes in the package and sub package for any bean file.
  • There is some example of auto configuration done by Spring Boot:
    • DispatcherServletAutoConfiguration
    • DataSourceAutoConfiguration
    • JacksonAutoConfiguration
    • ErrorMvcAutoConfiguration (#basicErrorController)
  • We can see the auto-configuration done by Spring Boot in the AUTO-CONFIGURATION REPORT or CONDITIONS EVALUATION REPORT.
  • Classes can be excluded from auto-configuration by adding:

Or add the following statement in the application.properties file.

We exclude classes form the auto-configuration for faster startup and better performance of the application.

  • AUTO-CONFIGURATION REPORT generated by enabling debug mode. Open the application.properties file and add the following statement:

Run RestfulWebServiceApplication.java file. It shows the Positive matches, Negative matches, Exclusions, and Unconditional classes under the CONDITIONS EVALUATION REPORT.

There is a lot of information inside the report, so it is not possible to show all the detail. If we scroll down the report and have a closure look at auto configuration log, we found DispatcherServletAutoConfiguration matched.

It is because, the dependency spring-boot-starter-web depends on spring-webmvc dependency. Therefore we get the DispatcherServlet class in our classpath.

Dispatcher Servlet

In Spring MVC all incoming requests go through a single servlet is called Dispatcher Servlet (front controller). The front controller is a design pattern in web application development. A single servlet receives all the request and transfers them to all other components of the application.

Spring Boot Auto Configuration and Dispatcher Servlet

The job of DispatcherServlet is to take an incoming URI and find the right combination of handlers (Controller classes) and views (usually JSPs). When the DispatcherServlet determines the view, it renders it as the response. Finally, the DispatcherServlet returns the Response Object to back to the client. In short, the Dispatcher Servlet plays the key role.

The other thing to notice is that ErrorMvcAutoConfiguration:

It configures the basicErrorController, errorAttributes, ErrorMvcAutoConfiguration, and DefaultErrorViewResolverConfiguration. It creates the default error page which is known as Whitelabel Error Page.

Spring Boot Auto Configuration and Dispatcher Servlet

The other thing which is auto-configured HttpMessageConvertersAutoConfiguration. These message converter automatically converts the message.

It initializes the Jackson bean and the message converter. The Jackson2ObjectMapper does the conversion from bean to JSON and JSON to bean.








Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA