实战笔记
- 编写Mvc配置类
@Configuration @EnableWebMvc @ComponentScan("com.yicj.study.webmvc.controller") public class WebConfig { }
- 编写App配置类
@Configuration @ComponentScan("com.yicj.study.webmvc.service") public class AppConfig { }
- 编写Java注解MVC启动类(WebApplicationInitializer接口实现类)
public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class[]{AppConfig.class};}@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class[]{WebConfig.class};}@Overrideprotected String[] getServletMappings() {return new String[]{"/"};}@Overrideprotected Filter[] getServletFilters() {return new Filter[]{new CharacterEncodingFilter("utf-8", true)};} }
启动流程与配置加载
- Servlet提供API: ServletContainerInitializer是容器启动的入口
- 其中SpringServletContainerInitializer继承ServletContainerInitializer,启动时会回调WebApplicationInitializer
- AbstractAnnotationConfigDispatcherServletInitializer继承WebApplicationInitializer并实现了onStartup方法
- 在步骤2的onStartup方法中初始化了SpringRoot根容器与SpringMvc容器
- 在构建SpringMvc容器的同时初始化了DispatcherServlet类,并自动回调init方法
- FrameworkServlet#initServletBean设置Spring根容器与SpringMvc容器的父子关系,并执行configureAndRefreshWebApplicationContext刷新
- 容器启动完成通过ContextRefreshListener事件监听回调FrameworkServlet#onApplicationEvent方法
- 在上一步的onApplicationEvent中调用DispatcherServlet#onRefresh(event.getApplicationContext())
- DispatcherServlet#initStrategies初始化SpringMvc相关的组件