tomcat6类加载器与类加载顺序

时间:2023-03-09 00:01:52
tomcat6类加载器与类加载顺序

tomcat6.0.32

com.dyyx.ShareUtils
//返回系统当前时间
public static String now();

package com.dyyx;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ShareUtils {
    private static final String FULL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String now() {
        Date now = new Date();
        DateFormat dateFormat = new SimpleDateFormat(FULL_DATE_FORMAT);
        return dateFormat.format(now);
    }
}

http://127.0.0.1:8080/tools/share_utils_now.jsp

share_utils_now.jsp 里调用 ShareUtils.now() 输出当前时间
并且会调用getClassLoadInfo(Class c)  方法 输出 ShareUtils.class 的类加载器信息 代码如下

public static String getClassLoadInfo(Class c) {
        String rname = getResourceName(c);
        URL url = null;
        ClassLoader cl = c.getClassLoader();
        // 如果ClassLoader为NULL 为系统类
        if (null == cl) {
            url = ClassLoader.getSystemResource(rname);
        } else {
            url = cl.getResource(rname);
        }
        String s = "class=" + c + ",classloader:" + cl + ",url=" + url;
        s = s + ",resourceName=" + rname;
        s = s + "\n\n";
        while (cl != null) {
            cl = cl.getParent();
            s = s + cl + "\n";

}
        return s;
    }

public static String getResourceName(Class clazz) {
        if (null == clazz) {
            return "class is null";
        }
        String cname = clazz.getName();
        String[] arr = cname.split("\\.");
        String rname = "";
        int len = arr.length;
        for (int i = 0; i < len; i++) {
            if (i > 0) {
                rname = rname + "/";
            }
            rname = rname + arr[i];
        }
        rname = rname + ".class";
        return rname;
    }

share-utils.jar  包含 ShareUtils.class

类加载顺序
${JAVA_HOME}/jre/lib/ext
java classpath 
${webapp}/WEB-INF/classes
${webapp}/WEB-INF/lib 
${tomcat.home}/lib

bootstrap class loader 引导类加载器 加载java核心类库 不是java.lang.ClassLoader的实例
ExtClassLoader 的父加载器为 bootstrap class loader  但是 getParent()拿到是null

把 share-utils.jar 或 ShareUtils.class 依次从相应的目录移除

以下为各种类加载情况日志记录

1 ExtClassLoader ${JAVA_HOME}/jre/lib/ext
class=class com.dyyx.ShareUtils,classloader:sun.misc.Launcher$ExtClassLoader@addbf1,url=jar:file:/D:/soft/jdk1.6.0_21/jre/lib/ext/share-utils.jar!/com/dyyx/ShareUtils.class,resourceName=com/dyyx/ShareUtils.class

null

2 AppClassLoader java classpath 
class=class com.dyyx.ShareUtils,classloader:sun.misc.Launcher$AppClassLoader@19821f,url=jar:file:/D:/tomcat6min/bin/share-utils.jar!/com/dyyx/ShareUtils.class,resourceName=com/dyyx/ShareUtils.class

null

3 WebappClassLoader ${webapp}/WEB-INF/classes

class=class com.dyyx.ShareUtils,classloader:WebappClassLoader
  context: 
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:

,url=file:/D:/soft/book/high/code/action/webroot/WEB-INF/classes/com/dyyx/ShareUtils.class,resourceName=com/dyyx/ShareUtils.class

null

4 WebappClassLoader ${webapp}/WEB-INF/lib

class=class com.dyyx.ShareUtils,classloader:WebappClassLoader
  context: 
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:

,url=jar:file:/D:/soft/book/high/code/action/webroot/WEB-INF/lib/share-utils.jar!/com/dyyx/ShareUtils.class,resourceName=com/dyyx/ShareUtils.class

null

5 StandardClassLoader ${tomcat.home}/lib

class=class com.dyyx.ShareUtils,classloader:org.apache.catalina.loader.StandardClassLoader@1172e08,url=jar:file:/D:/tomcat6min/lib/share-utils.jar!/com/dyyx/ShareUtils.class,resourceName=com/dyyx/ShareUtils.class

null

双亲委派模型

引导类加载器 扩展类加载器 应用(系统)类加载器 自定义类加载器(L1)  自定义类加载器(L2)  自定义类加载器(Ln) ......