最近一直在死磕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機制將文件中的類加載的。