jetty学习笔记-jetty classloader


java classloader回顾

以sun jdk为例

classloader主要负责的工作

  • find  class:找到class
  • Links class:class和文件link
  • define class:将二进制文件转换为Class的一个实例

classloader的find class的方式

  1. findLoadedClass:首先看是否已经被当前的classloader加载过了
  2. parent.loadClass:调用parent加载器来load,如果没有parent loader,直接调用native的findBootStrapClass*方法来获取
  3. findClass:如果上述都没有找到,调用当前findClass
Class c = findLoadedClass(name);
                if (c == null) {
                    try {
                        if (parent != null) {
                            c = parent.loadClass(name, false);
                        } else {
                            c = findBootstrapClass0(name);
                        }
                    } catch (ClassNotFoundException e) {
                        // If still not found, then invoke findClass in order
                        // to find the class.
                        c = findClass(name);
                    }
                }

classloader的分类

  • bootstraploader:native,jvm启动后启动,负责加载jre中的核心lib/class(包括下面两个classloader)。
  • ExtClassLoader:由bootstraploader加载,负责加载jre中的ext包,parent=null
  • AppClassLoader:由bootstraploader加载,负责加载classpath中的类,parent=ExtClassLoader
  • UserClassLoader:自定义classloader,通过url指定资源位置

context classloader

实现parent classloader装载子classloader url内的class的需求


jetty classloader

WebAppClassLoader.loadClass:

1. 查找已经在的class

Class<?> c= findLoadedClass(name);

2. 如果是parentfirst或者system_class并且不是server_class,则采用parentfirst

if (c == null && _parent!=null && (_context.isParentLoaderPriority() || system_class) && !server_class)
        {
            tried_parent= true;
            try
            {
                c= _parent.loadClass(name);
                if (Log.isDebugEnabled())
                    Log.debug("loaded " + c);
            }
            catch (ClassNotFoundException e)
            {
                ex= e;
            }
        }

3. 否则调用当前的findClass

 if (c == null)
        {
            try
            {
                c= this.findClass(name);
            }
            catch (ClassNotFoundException e)
            {
                ex= e;
            }
        }

4. 当前木找到,找parent

 if (c == null && _parent!=null && !tried_parent && !server_class )
            c= _parent.loadClass(name);

5. resolve

if (resolve)
            resolveClass(c);

如果是childfirst,则查找路劲是:

WebAppClassLoader-->bootstraploader-->ExtClassLoader-->AppClassLoader


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值