DispatcherServlet

初始化时机

默认是第一次请求的时候进行初始化
可以通过配置文件spring.mvc.servlet.load-on-startup=1 开启容器启动的时候即初始化
 
新增配置WebConfig
@Configuration @ComponentScan @PropertySource("classpath:application.properties") @EnableConfigurationProperties({WebMvcProperties.class, ServerProperties.class}) public class WebConfig { // ⬅️内嵌 web 容器工厂 @Bean public TomcatServletWebServerFactory tomcatServletWebServerFactory(ServerProperties serverProperties) { return new TomcatServletWebServerFactory(serverProperties.getPort()); } // ⬅️创建 DispatcherServlet @Bean public DispatcherServlet dispatcherServlet() { return new DispatcherServlet(); } }
 
@Controller public class Controller1 { private static final Logger log = LoggerFactory.getLogger(Controller1.class); @GetMapping("/test1") public ModelAndView test1() throws Exception { log.debug("test1()"); return null; }
 
启动容器
AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(WebConfig.class);
notion image
发现没有DispatcherServlet初始化的日志
访问localhost:8080/test1 接口后开始打印初始化日志
notion image
 
在配置类增加DispatcherServletRegistrationBean 配置文件增加 spring.mvc.servlet.load-on-startup = 1(只要大于0即可,数字大小决定顺序) 之后,容器启动后自动初始化DispatcherServlet
@Configuration @ComponentScan @PropertySource("classpath:application.properties") @EnableConfigurationProperties({WebMvcProperties.class, ServerProperties.class}) public class WebConfig { // ⬅️内嵌 web 容器工厂 @Bean public TomcatServletWebServerFactory tomcatServletWebServerFactory(ServerProperties serverProperties) { return new TomcatServletWebServerFactory(serverProperties.getPort()); } // ⬅️创建 DispatcherServlet @Bean public DispatcherServlet dispatcherServlet() { return new DispatcherServlet(); } // ⬅️注册 DispatcherServlet, Spring MVC 的入口 @Bean public DispatcherServletRegistrationBean dispatcherServletRegistrationBean( DispatcherServlet dispatcherServlet, WebMvcProperties webMvcProperties) { DispatcherServletRegistrationBean registrationBean = new DispatcherServletRegistrationBean(dispatcherServlet, "/"); registrationBean.setLoadOnStartup(webMvcProperties.getServlet().getLoadOnStartup()); return registrationBean; } }
 
 
 

初始化流程

DispatcherServlet 的初始化代码
/** * This implementation calls {@link #initStrategies}. */ @Override protected void onRefresh(ApplicationContext context) { initStrategies(context); } /** * Initialize the strategy objects that this servlet uses. * <p>May be overridden in subclasses in order to initialize further strategy objects. */ protected void initStrategies(ApplicationContext context) { initMultipartResolver(context);//初始化文件上传解析器 initLocaleResolver(context);//本地化信息国家地区语言 initThemeResolver(context); initHandlerMappings(context);//路径映射器 initHandlerAdapters(context);//针对不同controller的适配器 initHandlerExceptionResolvers(context);//异常处理器 initRequestToViewNameTranslator(context); initViewResolvers(context); initFlashMapManager(context); }

initHandlerMappings

notion image
DispatcherServlet.properties 提供默认配置
notion image
notion image
 
notion image