Java基础之ClassLoader类加载器简介

Posted Thinking in BigData

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java基础之ClassLoader类加载器简介相关的知识,希望对你有一定的参考价值。

classloader简介

1 classloader层次结构(父子关系)

Bootstrap(ClassLoader)

  • ExtClassLoader
    • AppClassLoader
      • XXXClassLoader

详见代码如下:

sun.misc.Launcher
public Launcher() {

        Launcher.ExtClassLoader var1;

        try {

            var1 = Launcher.ExtClassLoader.getExtClassLoader();

        } catch (IOException var10) {

            throw new InternalError("Could not create extension class loader", var10);

        }

 

        try {

            this.loader = Launcher.AppClassLoader.getAppClassLoader(var1);

        } catch (IOException var9) {

            throw new InternalError("Could not create application class loader", var9);

        }

2 classloader加载类过程

classloader在加载class时优先尝试通过parent classloader加载,如果加载不到,才尝试自己加载,即常说的“双亲委派”,

并且如果一个class依赖其他class,比如ClassA依赖ClassB,ClassB默认由ClassA的classloader来加载。

* <p> The methods and constructors of objects created by a class loader may
* reference other classes. To determine the class(es) referred to, the Java
* virtual machine invokes the {@link #loadClass <tt>loadClass</tt>} method of
* the class loader that originally created the class.

详见代码如下:

java.lang.ClassLoader
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 {

                    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基础之ClassLoader类加载器简介的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向类加载器 ClassLoader ( Android 的八种类加载器 | ClassLoader | BaseDexClassLoader | DexClassLoader )(代

ClassLoader简介

ClassLoader 功能简介

jvm之java类加载机制和类加载器(ClassLoader)的详解

Java ClassLoader基础知识

jvm之java类加载机制和类加载器(ClassLoader)的详解