Spring MVC启动过程
# Tomcat根据Servlet协议调用启动类
根据Spring MVC文档 (opens new window)提供的案例,可以通过编程或xml
方式配置DispatcherServlet
,之后由Tomcat
扫描到该servlet
,启动容器。
下面以编程式方式为例,探索Spring MVC
启动过程。
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(AppConfig.class);
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(context);
ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
查看WebApplicationInitializer
的文档注释,可以知道实现该接口的类会通过SPI
机制 (opens new window)被SpringServletContainerInitializer
检测到并实例化。
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList<>();
if (webAppInitializerClasses != null) {
for (Class<?> waiClass : webAppInitializerClasses) {
// Be defensive: Some servlet containers provide us with invalid classes,
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer)
ReflectionUtils.accessibleConstructor(waiClass).newInstance());
}
catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@HandlesTypes
、ServletContainerInitializer
均由Servlet
协议规定。
在spring-web
项目下可以找到文件resources/META-INF/services/javax.servlet.ServletContainerInitializer
org.springframework.web.SpringServletContainerInitializer
Tomcat
实现了Servlet
协议,通过SPI
机制实例化SpringServletContainerInitializer
,调用onStartUp
方法将@HandlesTypes
指定的类型的类作为入参传入,然后实例化,并调用WebApplicationInitializer
的onStartUp
方法。
# 初始化DispatcherServlet
指定注解自动注入方式的Spring
容器AnnotationConfigWebApplicationContext
,放入DispatcherServlet
中。
DispatcherServlet
与WebApplicationContext
容器关系如下图
DispatcherServlet
结构:
onStartUp
方法向ServletContext
中添加了DispatcherServlet
实例,作为一个Servlet
,Tomcat
会调用其init
方法。
接下来看init
方法如何实现的:
GenericServlet
和HttpServlet
都是Servlet
提供的类
GenericServlet#init
public void init(ServletConfig config) throws ServletException {
this.config = config;
this.init();
}
2
3
4
HttpServletBean
由Spring
提供,它重写了init
方法
HttpServletBean#init
public final void init() throws ServletException {
// 将启动参数设置到DispatchServlet中
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
if (!pvs.isEmpty()) {
try {
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this);
ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext());
bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment()));
initBeanWrapper(bw);
bw.setPropertyValues(pvs, true);
}
catch (BeansException ex) {
if (logger.isErrorEnabled()) {
logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex);
}
throw ex;
}
}
// 模板方法,交由子类重写
initServletBean();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
FrameworkServlet#initServletBean
protected final void initServletBean() throws ServletException {
getServletContext().log("Initializing Spring " + getClass().getSimpleName() + " '" + getServletName() + "'");
if (logger.isInfoEnabled()) {
logger.info("Initializing Servlet '" + getServletName() + "'");
}
long startTime = System.currentTimeMillis();
try {
//初始化web容器
this.webApplicationContext = initWebApplicationContext();
initFrameworkServlet();
}
catch (ServletException | RuntimeException ex) {
logger.error("Context initialization failed", ex);
throw ex;
}
//log...
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FrameworkServlet#initWebApplicationContext
protected WebApplicationContext initWebApplicationContext() {
//父容器 这里拿到的是空的
WebApplicationContext rootContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
WebApplicationContext wac = null;
if (this.webApplicationContext != null) {
// A context instance was injected at construction time -> use it
wac = this.webApplicationContext;
if (wac instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
//容器还未刷新,cwac.isActive()=false
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent -> set
// the root application context (if any; may be null) as the parent
cwac.setParent(rootContext);
}
//刷新容器
configureAndRefreshWebApplicationContext(cwac);
}
}
}
if (wac == null) {
// No context instance was injected at construction time -> see if one
// has been registered in the servlet context. If one exists, it is assumed
// that the parent context (if any) has already been set and that the
// user has performed any initialization such as setting the context id
wac = findWebApplicationContext();
}
if (wac == null) {
// No context instance is defined for this servlet -> create a local one
wac = createWebApplicationContext(rootContext);
}
if (!this.refreshEventReceived) {
// Either the context is not a ConfigurableApplicationContext with refresh
// support or the context injected at construction time had already been
// refreshed -> trigger initial onRefresh manually here.
synchronized (this.onRefreshMonitor) {
onRefresh(wac);
}
}
if (this.publishContext) {
// Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
}
return wac;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
FrameworkServlet#configureAndRefreshWebApplicationContext
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
//设置容器id...
wac.setServletContext(getServletContext());
wac.setServletConfig(getServletConfig());
wac.setNamespace(getNamespace());
// 注册监听器,监听容器刷新
wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
// 初始化参数配置
ConfigurableEnvironment env = wac.getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(getServletContext(), getServletConfig());
}
//空实现 容器刷新之前的回调
postProcessWebApplicationContext(wac);
//容器刷新之前的回调
applyInitializers(wac);
//刷新容器
wac.refresh();
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
容器刷新过程之前已经探索过,不再赘述。在容器刷新的最后一步finishRefresh
中会发布容器刷新事件,会被之前注册的监听器处理
private class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
FrameworkServlet.this.onApplicationEvent(event);
}
}
2
3
4
5
6
7
FrameworkServlet#onApplicationEvent
public void onApplicationEvent(ContextRefreshedEvent event) {
this.refreshEventReceived = true;
synchronized (this.onRefreshMonitor) {
onRefresh(event.getApplicationContext());
}
}
2
3
4
5
6
调用DisPatcherServlet
重写的onRefresh
方法
protected void onRefresh(ApplicationContext context) {
initStrategies(context);
}
protected void initStrategies(ApplicationContext context) {
//初始化文件上传处理器
initMultipartResolver(context);
//初始化国际化处理器 解析用户所处区域,用于做多语言支持
initLocaleResolver(context);
//初始化主题处理器
initThemeResolver(context);
//初始化URL映射处理器,用于解析URL与Controller方法的映射
initHandlerMappings(context);
//
initHandlerAdapters(context);
initHandlerExceptionResolvers(context);
initRequestToViewNameTranslator(context);
initViewResolvers(context);
initFlashMapManager(context);
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20