一、前言
关于springframework的spring-web包中org.springframework.web.SpringServletContainerInitializer启动容器类,实现了javax.servlet.ServletContainerInitializer容器接口,从而实现不通过web.xml 启动JavaEE项目,详情参见源码说明。
二、源码说明
package org.springframework.web;@b@@b@import java.lang.reflect.Modifier;@b@import java.util.LinkedList;@b@import java.util.List;@b@import java.util.Set;@b@import javax.servlet.ServletContainerInitializer;@b@import javax.servlet.ServletContext;@b@import javax.servlet.ServletException;@b@import javax.servlet.annotation.HandlesTypes;@b@import org.springframework.core.annotation.AnnotationAwareOrderComparator;@b@@b@@HandlesTypes({WebApplicationInitializer.class})@b@public class SpringServletContainerInitializer@b@ implements ServletContainerInitializer@b@{@b@ public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)@b@ throws ServletException@b@ {@b@ List initializers = new LinkedList();@b@@b@ if (webAppInitializerClasses != null)@b@ for (Class waiClass : webAppInitializerClasses)@b@ {@b@ if ((!(waiClass.isInterface())) && (!(Modifier.isAbstract(waiClass.getModifiers()))) && @b@ (WebApplicationInitializer.class@b@ .isAssignableFrom(waiClass)))@b@ try@b@ {@b@ initializers.add((WebApplicationInitializer)waiClass.newInstance());@b@ }@b@ catch (Throwable ex) {@b@ throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);@b@ }@b@@b@ }@b@@b@@b@ if (initializers.isEmpty()) {@b@ servletContext.log("No Spring WebApplicationInitializer types detected on classpath");@b@ return;@b@ }@b@@b@ servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");@b@ AnnotationAwareOrderComparator.sort(initializers);@b@ for (WebApplicationInitializer initializer : initializers)@b@ initializer.onStartup(servletContext);@b@ }@b@}