按照servlet3.0规范的规定,tomcat等web容器在启动的时候需要查看META-INF/services目录下的以javax.servlet.ServletContainerInitializer作为文件名的文件并加载文件中实现了ServletContainerInitializer接口的类
ServletContainerInitializerspring-web的实现为SpringServletContainerInitializer:
WEB-INF/services/ SpringServletContainerInitializer该类被@HandlesTypes(WebApplicationInitializer.class)所注解,所以该类的onStartup方法实现中可以以Set<Class<?>> webAppInitializerClasses参数接收到所有WebApplicationInitializer接口的实现类:
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
再看看WebApplicationInitializer的实现类有哪些:
WebApplicationInitializer的层级关系下面看看SpringServletContainerInitializer.onStartup具体的逻辑(相关性不强的代码我就不贴出来了,主要看逻辑):
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList<>();
//拿到所有WebApplicationInitializer的实现类,如果该类不是接口、抽象类那么反射创建该类实例
if (webAppInitializerClasses != null) {
for (Class<?> waiClass : webAppInitializerClasses) {
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);
}
}
}
}
//排序后依次执行onStartup方法
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
initializer.onStartup(servletContext);
}
}
根据上面代码的分析SpringServletContainerInitializer并没有执行具体的springIOC容器的加载和DispatcherServlet的注册,而是把具体的加载逻辑委托给了WebApplicationInitializer的实现类,但是WebApplicationInitializer的层级关系图显示,spring框架除了提供了三个抽象实现外并没有提供具体的实现类,所以需要开发者自己提供,我们先看最外层的封装AbstractAnnotationConfigDispatcherServletInitializer的注释(不喜欢英文的直接看后面翻译):
/**
- Base class for {@link org.springframework.web.WebApplicationInitializer}
- implementations that register a
- {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
- configured with annotated classes, e.g. Spring's
- {@link org.springframework.context.annotation.Configuration @Configuration} classes.
- <p>Concrete implementations are required to implement {@link #getRootConfigClasses()}
- and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}.
- Further template and customization methods are provided by
- {@link AbstractDispatcherServletInitializer}.
- <p>This is the preferred approach for applications that use Java-based
- Spring configuration.
*/
大概意思是说:这个类是WebApplicationInitializer接口的基础实现,目的是通过使用诸如@Configuration注解的配置类来注册DispatcherServlet(译者:也包括root application context),继承这个类的具体实现需要实现getRootConfigClasses、getServletConfigClasses和getServletMappings三个方法,并且继承这个类是使用基于注解配置的推荐方式。
通过上面提供的三个方法名我们可以推断,只需要通过类似
这种方式就可以配置spring的root 容器和DispatcherServlet使用的mvc容器并配置DispatcherServlet的路径映射信息了,相当简单。
下面分析原理,再次熟悉一下层级关系:
->WebApplicationInitializer
->AbstractContextLoaderInitializer
->AbstractDispatcherServletInitializer
->AbstractAnnotationConfigDispatcherServletInitializer
直接看onStartup方法,onStartup只在AbstractContextLoaderInitializer和AbstractDispatcherServletInitializer中实现,我们从外往里看
AbstractDispatcherServletInitializer:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//调用父类的onStartup
super.onStartup(servletContext);
//注册DispatcherServlet
registerDispatcherServlet(servletContext);
}
AbstractContextLoaderInitializer:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//注册ContextLoaderListener
registerContextLoaderListener(servletContext);
}
其实从类的名字上我们就可以猜出来他们各自完成了什么工作了,AbstractContextLoaderInitializer的作用是向servletContext中注册ContextLoaderListener,这和我们使用xml配置方式加载springIOC容器的方式是一样的,AbstractDispatcherServletInitializer负责注册DispatcherServlet加载mvc容器。
protected void registerContextLoaderListener(ServletContext servletContext) {
//创建根容器rootAppContext
WebApplicationContext rootAppContext = createRootApplicationContext();
if (rootAppContext != null) {
//实例化ContextLoaderListener
ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);
//必要的话传入contextInitializers
listener.setContextInitializers(getRootApplicationContextInitializers());
//注册到servletContext中
servletContext.addListener(listener);
}
else {
logger.debug("No ContextLoaderListener registered, as " +
"createRootApplicationContext() did not return an application context");
}
}
关于上面创建根容器rootAppContext的方法createRootApplicationContext:
protected WebApplicationContext createRootApplicationContext() {
//获取用户定义的根容器配置类,在我们的例子中是AppConfig
Class<?>[] configClasses = getRootConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
//仅仅将配置类注册到根IOC容器中以供ContextLoaderListener使用,具体容器加载过程由ContextLoaderListener完成
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
rootAppContext.register(configClasses);
return rootAppContext;
}
else {
return null;
}
}
ContextLoaderListener加载IOC容器的原理这里就不再赘述了。下面看DispatcherServlet的注册过程:
protected void registerDispatcherServlet(ServletContext servletContext) {
//获得servlet名称,固定值"dispatcher"
String servletName = getServletName();
//生成AnnotationConfigWebApplicationContext容器,并将配置类注册进去(具体代码看后面的代码块)
WebApplicationContext servletAppContext = createServletApplicationContext();
//实例化DispatcherServlet,传入生成好的context容器
FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
//必要的话传入contextInitializers
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
//将DispatcherServlet注册到servletContext中
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
//设置LoadOnStartup
registration.setLoadOnStartup(1);
//设置mapping
registration.addMapping(getServletMappings());
//设置异步支持
registration.setAsyncSupported(isAsyncSupported());
//注册过滤器
Filter[] filters = getServletFilters();
if (!ObjectUtils.isEmpty(filters)) {
for (Filter filter : filters) {
registerServletFilter(servletContext, filter);
}
}
//自定义配置
customizeRegistration(registration);
}
createServletApplicationContext:
protected WebApplicationContext createServletApplicationContext() {
//实例化容器
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
//获取配置类(webConfig)
Class<?>[] configClasses = getServletConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
//注册配置类
servletAppContext.register(configClasses);
}
return servletAppContext;
}
配置好DispatcherServlet后,服务器web容器启动时根据LoadOnStartup属性会自动开始springmvc容器的加载过程。
总结
web容器启动->
注册contextloaderlistener(此监听器为servletcontextlistener,在servletcontext初始化之后执行内部方法完成root context的初始化工作)
向web容器注册dispatcherservlet,设置loadonstartup参数为1,即启动时执行init方法,在init方法中获取之前初始化完成的root context 完成web context的初始化。
网友评论