首页 > 其他 > 详细

tomcat6类加载器与类加载顺序

时间:2014-07-18 00:19:16      阅读:483      评论:0      收藏:0      [点我收藏+]

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) ......

bubuko.com,布布扣

tomcat6类加载器与类加载顺序,布布扣,bubuko.com

tomcat6类加载器与类加载顺序

原文:http://www.cnblogs.com/metoy/p/3851770.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!