本文主要是介绍Java类加载器--Tomcat ClassLoader类加载器和WebappClassLoader类加载器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文Tomcat版本基于apache-tomcat-8.5.12.看一下Tomcat的源码Bootstrap类的initClassLoader方法,代码如下:
private void initClassLoaders() {try {// 创建commonLoader// 这里父加载器传递null,但是内部会使用默认的类加载器AppClassLoader作为父加载器.commonLoader = createClassLoader("common", null);if( commonLoader == null ) {// no config file, default to this loader - we might be in a 'single' env.commonLoader=this.getClass().getClassLoader();}// 创建catalinaLoader,父加载器为commonLoadercatalinaLoader = createClassLoader("server", commonLoader);// 创建sharedLoader,父类加载器为commonLoadersharedLoader = createClassLoader("shared", commonLoader);} catch (Throwable t) {handleThrowable(t);log.error("Class loader creation threw exception", t);System.exit(1);}
}
通过initClassLoaders方法可以得知上面三个加载器关系.下面具体看一下createClassLoader是如何构造类加载器的.代码如下:
private ClassLoader createClassLoader(String name, ClassLoader parent)throws Exception {// 获取catalina.properties文件中配置项分别为:common.loader/server.loader/shared.loader// 用来设置对应类加载器扫描类的路径.默认内容如下:String value = CatalinaProperties.getProperty(name + ".loader");if ((value == null) || (value.equals("")))return parent;// 依据common.loader/server.loader/shared.loader的配置装饰类加载器所需的扫描路径.value = replace(value);List<Repository> repositories = new ArrayList<>();String[] repositoryPaths = getPaths(value);for (String repository : repositoryPaths) {// Check for a JAR URL repositorytry {@SuppressWarnings("unused")URL url = new URL(repository);repositories.add(new Repository(repository, RepositoryType.URL));continue;} catch (MalformedURLException e) {// Ignore}// Local repositoryif (repository.endsWith("*.jar")) {repository = repository.substring(0, repository.length() - "*.jar".length());repositories.add(new Repository(repository, RepositoryType.GLOB));} else if (repository.endsWith(".jar")) {repositories.add(new Repository(repository, RepositoryType.JAR));} else {repositories.add(new Repository(repository, RepositoryType.DIR));}}return ClassLoaderFactory.createClassLoader(repositories, parent);
}
common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar"
server.loader=
shared.loader=
所以依据上面的代码可知,commonLoader/serverLoader/sharedLoader是同一个ClassLoader.
具体创建类加载器的是ClassLoaderFactory.createClassLoader(repositories, parent);
,具体代码如下:
public static ClassLoader createClassLoader(List<Repository> repositories,final ClassLoader parent)throws Exception {if (log.isDebugEnabled())log.debug("Creating new class loader");// Construct the "class path" for this class loaderSet<URL> set = new LinkedHashSet<>();if (repositories != null) {for (Repository repository : repositories) {if (repository.getType() == RepositoryType.URL) {URL url = buildClassLoaderUrl(repository.getLocation());if (log.isDebugEnabled())log.debug(" Including URL " + url);set.add(url);} else if (repository.getType() == RepositoryType.DIR) {File directory = new File(repository.getLocation());directory = directory.getCanonicalFile();if (!validateFile(directory, RepositoryType.DIR)) {continue;}URL url = buildClassLoaderUrl(directory);if (log.isDebugEnabled())log.debug(" Including directory " + url);set.add(url);} else if (repository.getType() == RepositoryType.JAR) {File file=new File(repository.getLocation());file = file.getCanonicalFile();if (!validateFile(file, RepositoryType.JAR)) {continue;}URL url = buildClassLoaderUrl(file);if (log.isDebugEnabled())log.debug(" Including jar file " + url);set.add(url);} else if (repository.getType() == RepositoryType.GLOB) {File directory=new File(repository.getLocation());directory = directory.getCanonicalFile();if (!validateFile(directory, RepositoryType.GLOB)) {continue;}if (log.isDebugEnabled())log.debug(" Including directory glob "+ directory.getAbsolutePath());String filenames[] = directory.list();
这篇关于Java类加载器--Tomcat ClassLoader类加载器和WebappClassLoader类加载器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!