[tomcat7源码学习]ClassLoader加载Tomcat的依赖

ClassLoader加载Tomcat的依赖

经过查看catalina.bat中设置的classpath只有bootstrap.jartomcat-juli.jar,这两个都只包含Bootstrap执行时所须要的类,因此若是要进行后续操做,都须要再加载。经过前面返回的ClassLoader加载apache

1.在BootStrap中加载完成ClassLoader以后就回到了init方法中.bootstrap

Thread.currentThread().setContextClassLoader(catalinaLoader);

    SecurityClassLoad.securityClassLoad(catalinaLoader);

这里就开始加载tomcat所须要的classtomcat

2.进入SecurityClassLoad.securityClassLoad测试

public static void securityClassLoad(ClassLoader loader)
        throws Exception {

    if( System.getSecurityManager() == null ){
        return;
    }

    loadCorePackage(loader);
    loadCoyotePackage(loader);
    loadLoaderPackage(loader);
    loadRealmPackage(loader);
    loadSessionPackage(loader);
    loadUtilPackage(loader);
    loadValvesPackage(loader);
    loadJavaxPackage(loader);
    loadConnectorPackage(loader);
    loadTomcatPackage(loader);
}

就能够大概看出加载class的顺序了,各个方法里面都是加载指定的Class,也就是要加载那些类都写死了,若是咱们要作什么扩展,只是把写好的类扔到包中,应该就会出问题。。优化

###总结###this

1.对于tomcat本身要使用的类都是指定类名进行加载的spa

2.在查看启动时classpath的设置过程当中,反编译了一下生成的BootStrap.class,和源码相比发现其中少import了一个的类:org.apache.catalina.Globals,开始还觉得是在编译过程当中作了什么处理,最后查看这个类是final的,再到网上一查,原来在编译时会对final作优化,会把final变量名直接替换成其相应的值,而后本身也作了一个小测试,确实如此。。。。看来之前疏忽了。。。code

3.在回头查看ClassLoader的过程当中,发现每一个Class都会先判断,也就是已经加载了的,就不会再加载rem

Class c = findLoadedClass(name);
        if (c == null) {
            long t0 = System.nanoTime();
            try {
                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);
        }

先调用了findLoadedClass,若是没有,才继续查找get

相关文章
相关标签/搜索