Tomcat类载入器

       大家都知道,Java的类加载机制是双亲委派模型,那么什么是双亲委派模型呢?我们这里简要的说一下,双亲委派模型就是说把类的加载委托给它的父类加载器去加载,父加载器委托给它的祖父加载器去加载,一直这样,直到它的父加载器是null为止,类加载器有如下的几种类型:

image.jpeg

类加载器的源代码如下,从中,我们可以很清楚的看到类加载委派的过程:

protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    //这里就是委派过程,直到父类加载器为null为止
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                // ClassNotFoundException thrown if class not found
                // from the non-null parent class loader
                }
                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    c = findClass(name);
                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

       好了,Java的类加载机制就说道这里,毕竟我们这里是看看tomcat的类载入器,下面进入主题。参考书籍(深入剖析tomcat)。
       tomcat必须实现一个自己的类加载器,这是因为我们并不能完全信任servlet容器,他应该只能加载自己WEB-INF/classes目录下的类和部署到WEB-INF/lib中的类,而在tomcat中,这个类必须实现org.apache.catalina.Loader。先来看看Loader接口的定义:

public interface Loader {
        //这个方法在后面说
        public void backgroundProcess();
        //获取ClassLoader实例
        public ClassLoader getClassLoader();
        //获取Context
        public Context getContext();

        public void setContext(Context context);

        public boolean getDelegate();
        //指明是否要委托给一个父类加载器
        public void setDelegate(boolean delegate);

        public boolean getReloadable();

        public void setReloadable(boolean reloadable);

        public void addPropertyChangeListener(PropertyChangeListener listener);
        //获取tomcat里的类是否发生了修改

        public boolean modified();

        public void removePropertyChangeListener(PropertyChangeListener listener);

    }

       我们主要看modified()方法,这个方法是用来获取tomcat里的类是否发生了修改,我们都知道,我们可以通过配置来修改tomcat是否支持热启动,那么它的原理是什么呢?它通过一个线程来周期性的调用modified()方法,来确定类是否发生了修改,如果发生了,那么就会调用context的reload方法来自动重载,我们可以去org.apache.catalina.core.ContainerBase类中找到run()方法,其由内部类ContainerBackgroundProcessor实现,它周期的调用org.apache.catalina.core.StandardContext下的backgroundProcess()方法,而这个backgroundProcess 方法也是Loader接口中声明的方法,这个方法如下所示:

@Override
    public void backgroundProcess() {
        if (reloadable && modified()) {
            try {
                Thread.currentThread().setContextClassLoader(WebappLoader.class.getClassLoader());
                if (context != null) {
                    //发生了修改,这调用context的reload方法,重新加载容器
                    context.reload();
                }
            } finally {
                if (context != null && context.getLoader() != null) {
                    Thread.currentThread().setContextClassLoader(context.getLoader().getClassLoader());
                }
            }
        }
    }

       至此,可以看到tomcat是如何支持热启动的。
       在tomcat中,负责载入类的是WebappClassLoader,它的父类中定义了一些不能载入的类,比如以javax开头的类,具体可以到WebappClassLoaderBase类中的filter方法中查看。当然,为了达到更好的性能,会缓存已经载入的类,下次使用的时候就可以直接获取。
       在载入类的时候,WebappClassLoader会遵守如下的规则:
       1)因为所有已经载入的类都会缓存起来,所以载入类时要先检查本地缓存;
       2)若本地缓存中没有,则检查上一层缓存,即调用 java.lang.ClassLoader 类的findLoadedClass() 方法;
       3)若两个缓存中都没有,则使用系统的类载入器进行加载,防止 web 应用程序中的类覆盖J2EE 的类;
       4)若启用了 SecurityManager,则检查是否允许载入该类。若该类是禁止载入的类,抛出 ClassNotFoundException异常;
       5)若打开标志位 delegate,或者待载入的类是属于包触发器中的包名,则调用父载入器来载入相关类。如果父载入器是null,则使用系统的类载入器;
       6)从当前仓库中载入相关的类;
       7)若当前仓库中没有需要的类,且标志位delegate关闭,则使用父类载入器。若父类载入器为 null, 则使用系统的类载入器进行加载;
       8)若仍未找到需要的类,则抛出 ClassNotFoundException 异常;
       至此,结束。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容