美文网首页
Android ClasslLoader双亲委派加载机制

Android ClasslLoader双亲委派加载机制

作者: 图之 | 来源:发表于2021-01-13 15:57 被阅读0次

双亲委派的优点:
如果有人想要篡改程序的类实现,在这种机制下是无效的,因为这些系统的类已经被BootStrapClassLoader加载过了,不会再次加载,从一定程度上防止了危险代码的植入。

java文件编译完全会生成 .class文件, .class文件就是通过类加载器ClassLoader加载的。ClassLoader在加载过程中会用双亲委派机制加载 .class文件。

一一一一一一一一一一一
CustomClassLoader ( 自己搞的,可以不用,用appClassLoader基本够用)
一一一一一一一一一一一
👇
一一一一一一一一一一一
AppClassLoader (应用程序类加载器,appclassloader会加载*Java环境变量 CLASSPATH所指定的路径下的类库 * ,而CLASSPATH所指定的路径可以通过 System.getProperty("java.class.path") 获取,该变量可被覆盖,可以使用 -cp 参数)
一一一一一一一一一一一
👇
一一一一一一一一一一一
ExtClassLoader(扩展类加载器,会加载 ( $ JAVA_HOME/jre/lib/ext)下的类库)

一一一一一一一一一一一
👇
一一一一一一一一一一一
BootStrapClassLoader(启动类加载器,JVM启动时创建,用于加载 JAVA_HOME /JRE/LIB 下面的类库)
一一一一一一一一一一一

ClassLoader的双亲委派机制:
1.当appclassloader加载一个class时,会先把类加载请求委派给父类的加载器ExtClassLoader去完成加载;
2.当ExtClassLoader加载一个class时,会先把类加载请求委派给父类的加载器BootStrapClassLoader去完成加载;
3.如果BootStrapClassLoader加载失败,会使用ExtClassLoader来尝试加载;
4.如果ExtClassLoader加载失败,会使用AppClassLoader来 加载,如果AppClassLoader也加载失败,则会抛出异常ClassNotFoundException

  /**
     * Loads the class with the specified <a href="#name">binary name</a>.  The
     * default implementation of this method searches for classes in the
     * following order:
     *
     * <ol>
     *
     *   <li><p> Invoke {@link #findLoadedClass(String)} to check if the class
     *   has already been loaded.  </p></li>
     *
     *   <li><p> Invoke the {@link #loadClass(String) <tt>loadClass</tt>} method
     *   on the parent class loader.  If the parent is <tt>null</tt> the class
     *   loader built-in to the virtual machine is used, instead.  </p></li>
     *
     *   <li><p> Invoke the {@link #findClass(String)} method to find the
     *   class.  </p></li>
     *
     * </ol>
     *
     * <p> If the class was found using the above steps, and the
     * <tt>resolve</tt> flag is true, this method will then invoke the {@link
     * #resolveClass(Class)} method on the resulting <tt>Class</tt> object.
     *
     * <p> Subclasses of <tt>ClassLoader</tt> are encouraged to override {@link
     * #findClass(String)}, rather than this method.  </p>
     *
     *
     * @param  name
     *         The <a href="#name">binary name</a> of the class
     *
     * @param  resolve
     *         If <tt>true</tt> then resolve the class
     *
     * @return  The resulting <tt>Class</tt> object
     *
     * @throws  ClassNotFoundException
     *          If the class could not be found
     */
    // Android-removed: Remove references to getClassLoadingLock
    //                   Remove perf counters.
    //
    // <p> Unless overridden, this method synchronizes on the result of
    // {@link #getClassLoadingLock <tt>getClassLoadingLock</tt>} method
    // during the entire class loading process.
    protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                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.
                    c = findClass(name);
                }
            }
            return c;
    }

打破双亲委派机制:
https://blog.csdn.net/cy973071263/article/details/104129163

相关文章

  • Android ClasslLoader双亲委派加载机制

    双亲委派的优点:如果有人想要篡改程序的类实现,在这种机制下是无效的,因为这些系统的类已经被BootStrapCla...

  • 从类加载开始的JVM学习

    目录 引言 java类加载流程 java类加载机制- 类加载原理- 双亲委派机制 Tomcat中双亲委派机制的应用...

  • Android_类加载机制之双亲委派

    本文目标 深入理解Android的类加载机制 1.什么是双亲委派 2.双亲委派下的Class文件加载流程 3.An...

  • 为什么使用双亲委派机制?

    为什么使用双亲委派机制? 专业名词 说双亲委派机制就不得不说类加载器。 引导类加载器:加载%JAVA_HOME%/...

  • 四、初识ClassLoader

    双亲委派的定义 类加载器的父亲委托机制(双亲委派机制) 类加载器加载一个类时,会优先交给其父的加载器加载,父加载器...

  • 类加载时机和生命周期_看图说话

    类加载时机 类生命周期 类加载机制 双亲委派机制的核心,ClassLoader.loadClass(): 这种委派...

  • SPI的ClassLoader问题

    问题 为什么说spi服务机制破坏了双亲委派模型? 双亲委派机制 启动类加载器(Bootstrap ClassLoa...

  • 3.手写自己的java类加载器

    类的加载过程?何为双亲委派机制?为啥这么设计?实现一个自己的类加载器?如何打破双亲委派机制? 1.类加载器 jar...

  • Android ClassLoader原理(一)

    Android的类加载机制遵循Java的双亲委派原理。其继承关系如下: PathClassLoader和DexCl...

  • Java虚拟机

    JVM 组成部分 类加载器 执行引擎 内存区 本地方法调用 类加载器 双亲委派模型 类的加载过程采用双亲委派机制,...

网友评论

      本文标题:Android ClasslLoader双亲委派加载机制

      本文链接:https://www.haomeiwen.com/subject/aanuaktx.html