最近一直在死磕Tomcat 9 的源码,在此写下一些自己的学习心得,大家可以一起学习,互相讨论。
本文主要是关于 Tomcat中 ServletContainerInitializer 初始化的部分。
先看一张截图:
1. 左侧为方法调用栈信息
2.中间部分是一个loader加载器获取资源路径信息
此处贴出源码:此处的源码是说明 tomcat 加载定义的 ServletContainerInitializer 的实现类的来源,是通过 解析META-INF/services/javax.servlet.ServletContainerInitializer 文件中的权限的类名读取到集合中进行解析。
public class WebappServiceLoader<T> {
private static final String LIB = "/WEB-INF/lib/";
private static final String SERVICES = "META-INF/services/";
private final Context context;
private final ServletContext servletContext;
private final Pattern containerSciFilterPattern;
/**
* Construct a loader to load services from a ServletContext.
*
* @param context the context to use
*/
public WebappServiceLoader(Context context) {
this.context = context;
this.servletContext = context.getServletContext();
String containerSciFilter = context.getContainerSciFilter();
if (containerSciFilter != null && containerSciFilter.length() > 0) {
containerSciFilterPattern = Pattern.compile(containerSciFilter);
} else {
containerSciFilterPattern = null;
}
}
/**
* Load the providers for a service type.
*
* @param serviceType the type of service to load
* @return an unmodifiable collection of service providers
* @throws IOException if there was a problem loading any service
*/
public List<T> load(Class<T> serviceType) throws IOException {
// String SERVICES = "META-INF/services/";
String configFile = SERVICES + serviceType.getName();
LinkedHashSet<String> applicationServicesFound = new LinkedHashSet<>();
LinkedHashSet<String> containerServicesFound = new LinkedHashSet<>();
ClassLoader loader = servletContext.getClassLoader();
// if the ServletContext has ORDERED_LIBS, then use that to specify the
// set of JARs from WEB-INF/lib that should be used for loading services
@SuppressWarnings("unchecked")
List<String> orderedLibs =
// javax.servlet.context.orderedLibs
(List<String>) servletContext.getAttribute(ServletContext.ORDERED_LIBS);
if (orderedLibs != null) {
// handle ordered libs directly, ...
for (String lib : orderedLibs) {
URL jarUrl = servletContext.getResource(LIB + lib);
if (jarUrl == null) {
// should not happen, just ignore
continue;
}
String base = jarUrl.toExternalForm();
URL url;
if (base.endsWith("/")) {
url = new URL(base + configFile);
} else {
url = JarFactory.getJarEntryURL(jarUrl, configFile);
}
try {
parseConfigFile(applicationServicesFound, url);
} catch (FileNotFoundException e) {
// no provider file found, this is OK
}
}
// and the parent ClassLoader for all others
loader = context.getParentClassLoader();
}
Enumeration<URL> resources;
if (loader == null) {
resources = ClassLoader.getSystemResources(configFile);
} else {
resources = loader.getResources(configFile);
}
while (resources.hasMoreElements()) {
// 解析 文件中的类信息 META-INF/services/目录下 javax.servlet.ServletContainerInitializer命名的文件
parseConfigFile(containerServicesFound, resources.nextElement());
}
// Filter the discovered container SCIs if required
if (containerSciFilterPattern != null) {
Iterator<String> iter = containerServicesFound.iterator();
while (iter.hasNext()) {
if (containerSciFilterPattern.matcher(iter.next()).find()) {
iter.remove();
}
}
}
// Add the application services after the container services to ensure
// that the container services are loaded first
containerServicesFound.addAll(applicationServicesFound);
// load the discovered services
if (containerServicesFound.isEmpty()) {
return Collections.emptyList();
}
// 将 javax.servlet.ServletContainerInitializer文件中的 class全路径名保存到containerServicesFound 集合中
// 调用下面的方法反射创建对象,最后返回 List
return loadServices(serviceType, containerServicesFound);
}
void parseConfigFile(LinkedHashSet<String> servicesFound, URL url)
throws IOException {
try (InputStream is = url.openStream();
InputStreamReader in = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(in)) {
String line;
while ((line = reader.readLine()) != null) {
int i = line.indexOf('#');
if (i >= 0) {
line = line.substring(0, i);
}
line = line.trim();
if (line.length() == 0) {
continue;
}
servicesFound.add(line);
}
}
}
List<T> loadServices(Class<T> serviceType, LinkedHashSet<String> servicesFound)
throws IOException {
ClassLoader loader = servletContext.getClassLoader();
List<T> services = new ArrayList<>(servicesFound.size());
for (String serviceClass : servicesFound) {
try {
Class<?> clazz = Class.forName(serviceClass, true, loader);
services.add(serviceType.cast(clazz.getConstructor().newInstance()));
} catch (ReflectiveOperationException | ClassCastException e) {
throw new IOException(e);
}
}
return Collections.unmodifiableList(services);
}
}
WebappServiceLoader # List<T> load(Class<T> serviceType) 方法调用栈信息:
此调用栈信息很清晰的说明了tomcat启动时的一个类 的 方法调用过程以及调用顺序。
StandardContext #startInternal()
==》
StandardContext父类LifecycleBase #fireLifecycleEvent(Lifecycle.CONFIGURE_START_EVENT, null);
==》
ContextConfig#lifecycleEvent(event) 此处LifecycleEvent event = new LifecycleEvent(this, type, data); ContextConfig何时被加载到StandardContext中的后面的文章我们在分析。
==》
ContextConfig #configureStart();
==》
ContextConfig #webConfig();
==》
ContextConfig #processServletContainerInitializers
==》
WebappServiceLoader #load(Class<T> serviceType)
至此加载ServletContainerInitializer完成 ,最后 ServletContainerInitializer的实现类会在StandardContext #startInternal() 方法的最后面进行调用。
自定义一个ServletContainerInitializer
此处我自定义一个 ServletContainerInitializer ,tomcat启动时,会将我定义的MyServletContainerInitializer加载到集合中,在StandardContex 调用start() 方法时,便会出发MyServletContainerInitializer类中的 onStartup(...) 方法。
package gao.test.tomcat.servletcontainerInitializer;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import org.springframework.web.WebApplicationInitializer;
@HandlesTypes(WebApplicationInitializer.class)
public class MyServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup( 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); }
*/
System.out.println("---------------------------"+waiClass.getName()+"--------------------------");
}else {
System.out.println("------------******---------------"+waiClass.getName()+"-----------------****---------");
}
}
}
}
}
这是调用的一个结果,以及打印的日志信息。
很明显这个是通过java 的SPI机制将文件中的类加载的。