SpringMVC的本质就是一个dispatcherServlet。


DispatcherServlet的创建过程主要是对9大组件进行初始化。DispatcherServlet的onRefresh方法调用了initStrategies方法,在initStrategies中初始化了组件。

    // 两个方法的参数context是WebApplicationContext,而不是ServletContext。
   // DispatcherServlet的父类FrameworkServlet初始化了WebApplicationContext。
protected void onRefresh(ApplicationContext context) { this.initStrategies(context); } protected void initStrategies(ApplicationContext context) { this.initMultipartResolver(context); this.initLocaleResolver(context); this.initThemeResolver(context); this.initHandlerMappings(context); this.initHandlerAdapters(context); this.initHandlerExceptionResolvers(context); this.initRequestToViewNameTranslator(context); this.initViewResolvers(context); this.initFlashMapManager(context); }

初始化某个组件时,首先在容器里按注册时的名称或类型查找(所以在springMVC的配置文件中只需要配置相应类型的组件容器就可以自动找到),如果找不到就调用getDefaultStrategy使用默认的组件。

当使用<mvc:annotation-driven/>后,就不会全部使用默认组件了,因为它配置了HandlerMapping、HandlerAdapter、HandlerExceptionResolver。

上传文件组件MultipartResolver是没有默认配置的,因为并不是每个应用都需要上传文件,需要上传也不一定就要使用MultipartResolver。


 

相关文章:

  • 2021-11-23
  • 2021-05-08
  • 2021-11-02
  • 2022-01-23
  • 2022-01-16
  • 2021-09-20
猜你喜欢
  • 2022-12-23
  • 2021-07-27
  • 2021-06-03
  • 2021-05-01
  • 2021-11-03
  • 2021-06-17
  • 2021-07-06
相关资源
相似解决方案