Springboot project tips - initializing spring dispatcherservlet 'dispatcherservlet' solution

LIYITONGXUEJune 14, 2022
Less than 1 minute...

一、Background

After running the springboot project, when using postman to test the interface, the console of idea has the following prompt:

Initializing Spring DispatcherServlet 'dispatcherServlet'
Initializing Servlet 'dispatcherServlet'
Completed initialization in 1 ms

二、Cause Analysis

The default value of spring boot load on startup is -1. When the project is started, the dispatcherservlet will not be initialized by default, that is, the init () method of the servlet interface will not be called.

三、Resolvent

Set spring.mvc.servlet.load-on-startup to 0 or a positive integer, and execute initialization when the project starts.

Method1、Add the following configuration in the application.properties configuration file

spring.mvc.servlet.load-on-startup=1

Method2、Add the following configuration in the application.yml configuration file

spring:
  mvc:
    servlet:
      load-on-startup: 1

Methods 1 and 2 are equivalent to adding the following configuration in the web.xml file of the spring MVC project:

  <servlet>
  	<servlet-name>dispatcher</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<async-supported>true</async-supported>
  	<load-on-startup>8</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>dispatcher</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>

The End

After re running the project, call the interface, and the idea console has no relevant prompt of dispatcherservlet, which is solved successfully.

Last update: 8/2/2022, 2:39:25 AM
Comments
Powered by Waline v2.6.2