1. 类加载器的介绍

通过一个类的全限定名来获取描述该类的二进制流。主要实现的目的是:

  • 实现类的加载动作

对于任何一个类,都必须由加载它的类加载器和这个类本身一起共同确定其在java虚拟机中的唯一性。每一个类加载器,都有一个独立的类名称空间。

通俗讲:比较两个类是否“相等”,只有在两个类是由同一个类加载器加载的前提下才有意义,否则,即使是一个Class文件,被同一个虚拟机加载,只要加载器不同,那么这两个类就必定不相等!

1. JDK9之前(主要是JDK8)

类加载器的总体分类关系可以有几种:

  • 严格意义上的分类
  • 广义上的分类

    1.1 严格意义上分类

    严格意义上分类也就是从Java虚拟机角度的分类。JVM严格来讲支持两种类型的类加载器 。分别为:

  • 引导类加载器(Bootstrap ClassLoader)

  • 自定义类加载器(User-Defined ClassLoader)

    1.1.1 引导类加载器

    引导类加载器,也叫启动类加载器。特点是:

  • 加载器使用 C++进行实现的

  • 引导类加载器属于虚拟机本身的一部分。

    1.1.2 自定义类加载器

    从概念上来讲,自定义类加载器一般指的是程序中由开发人员自定义的一类类加载器,但是Java虚拟机规范却没有这么定义,而是将所有派生于抽象类ClassLoader的类加载器都划分为自定义类加载器。(通俗讲就是,所有继承自**ClassLoader**的类加载器都是自定义类加载器。)

自定义加载器的特点:

  • 使用java语言实现,独立存在于虚拟机外部
  • 全部继承自抽象类 java.lang.ClassLoader。

    1.2 广义上的分类

    我们广义上分的话,加载器分为四类

  • 引导类加载器:Bootstrap ClassLoader,是用非java语言编写的,使用C/C++语言等编写的。

    1. 引导类加载器主要加载的是java的核心类库。
      1. 主要负责加载<JAVA_HOME>\lib 目录中,或者被 -Xbootclasspath 参数所指定的路径中存放的,并且是java虚拟机能够识别的(按照文件名识别,如:rt.jar、tools.jar,名字不符合的类即使放入lib目录也不会被加载)。
      2. 加载jre/lib/endorsed:这个主要是java的扩展技术,叫覆盖系统类:endorsed 技术。在这个包下的类,如果与系统类一样,则会覆盖系统类。所以需要使用引导类加载器进行加载。
    2. 需要注意的是:
      1. 启动类加载器无法被java程序直接引用。
      2. 我们编写自定义加载器时,如果需要把加载请求委托给引导类加载器去加载,那么直接用null代替即可。

关于引导类加载器(启动类加载器)的说明

下面代码是我们通过一个类获取其ClassLoader的时候的代码。

  • 第30行中:获取改类的ClassLoader。如果获取到为null,则直接返回null。
  • 在用户编写自定义加载器的时候,如果需要引导类加载器(启动类加载器)去进行加载的话,我们直接使用null值进行替代。就像注释中写的:Some implementations may use null to represent the bootstrap class loader.This method will return null in such implementations if this class was loaded by the bootstrap class loader. ```java /**
    • Returns the class loader for the class. Some implementations may use
    • null to represent the bootstrap class loader. This method will return
    • null in such implementations if this class was loaded by the bootstrap
    • class loader. *
    • If a security manager is present, and the caller’s class loader is

    • not null and the caller’s class loader is not the same as or an ancestor of
    • the class loader for the class whose class loader is requested, then
    • this method calls the security manager’s {@code checkPermission}
    • method with a {@code RuntimePermission(“getClassLoader”)}
    • permission to ensure it’s ok to access the class loader for the class. *
    • If this object

    • represents a primitive type or void, null is returned. *
    • @return the class loader that loaded the class or interface
    • represented by this object.
    • @throws SecurityException
    • if a security manager exists and its
    • {@code checkPermission} method denies
    • access to the class loader for the class.
    • @see java.lang.ClassLoader
    • @see SecurityManager#checkPermission
    • @see java.lang.RuntimePermission */ @CallerSensitive public ClassLoader getClassLoader() { // 获取改类的ClassLoader。如果获取到为null,则直接返回null ClassLoader cl = getClassLoader0(); if (cl == null) return null; SecurityManager sm = System.getSecurityManager(); if (sm != null) { ClassLoader.checkClassLoaderPermission(cl, Reflection.getCallerClass()); } return cl; }

// Package-private to allow ClassLoader access ClassLoader getClassLoader0() { return classLoader; }

  1. ---
  2. - **扩展类加载器**: `Extension ClassLoader`,继承自`ClassLoader`类。是jdk中`rt.jar`下`sun.misc.Launcher$ExtClassLoader`的一个内部类。
  3. - 主要是负责加载 <JAVA_HOME>\lib\ext目录,或者被`java.ext.dirs`系统变量所执行的路径中的所有类库。
  4. - 扩展类加载器主要加载一些jdk的扩展包:`jre/lib/ext`目录下的文件来进行jre的扩展。
  5. - **系统类加载器**:也叫应用程序类加载器,`AppClassLoader`,继承自`ClassLoader`类。类路径是`sun.misc.Launcher.AppClassLoader`
  6. - 系统类加载器主要加载我们通常写的一些类,业务类等。
  7. - 主要加载的类是:用户类路径下(Classpath下)的所有类库。
  8. - **自定义类加载器**:`User-Defined ClassLoader`,继承自`ClassLoader`类。
  9. **也就是说,只要是继承于**`**ClassLoader**`**类的类加载器,都属于自定义类加载器。也就是说:**`扩展类加载器`和`系统类加载器`都属于自定义类加载器。
  10. 1. 无论类加载器的类型如何划分,在程序中我们最常见的类加载器始终只有3个,如下所示<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/21593954/1647400563445-d1457479-e5f2-47a6-9344-70628be13c6d.png#clientId=ufa54d72d-ef9e-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=InR2d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=624&originWidth=1140&originalType=url&ratio=1&rotation=0&showTitle=false&size=249433&status=done&style=none&taskId=ud6f48c22-63dc-418d-9682-5b55076b85d&title=)
  11. 1. 关于这里的四者,指的是四种加载器:
  12. 1. 引导类加载器:`Bootstrap ClassLoader`、
  13. 1. 扩展类加载器: `Extension ClassLoader`、
  14. 1. 系统类加载器:也叫应用程序类加载器,`System ClassLoader`、
  15. 1. 自定义类加载器:`User-Defined ClassLoader`这四种。
  16. 3. 关于几种加载器的获取
  17. ```java
  18. public static void main(String[] args) {
  19. // 系统类加载器
  20. ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
  21. System.out.println(systemClassLoader);
  22. // 获取上层类 扩展类 加载器
  23. ClassLoader extClassLoader = systemClassLoader.getParent();
  24. System.out.println(extClassLoader);
  25. // 获取上层 引导类加载器
  26. ClassLoader bootstrapClassLoader = extClassLoader.getParent();
  27. System.out.println(bootstrapClassLoader);
  28. // sun.misc.Launcher$AppClassLoader@18b4aac2
  29. // sun.misc.Launcher$ExtClassLoader@2f2c9b19
  30. // null - 因为引导类加载器是用C等语言写的,我们无法获取到,所以是null
  31. // 用户自定义类的加载器是什么呢
  32. ClassLoader classLoader = ClassLoaderTest.class.getClassLoader();
  33. System.out.println(classLoader);
  34. // 使用的是系统类加载器 sun.misc.Launcher$AppClassLoader@18b4aac2
  35. // 那么 java的核心类库呢?比如:String类等
  36. // 这里我们并没有获取到,得到是null
  37. // 那么就应该是使用的 引导类加载器 进行加载的。
  38. ClassLoader stringClassLoader = String.class.getClassLoader();
  39. System.out.println(stringClassLoader);
  40. }

通过上面的代码我们可以知道:

  • JAVA的核心代码:是通过引导类加载器进行加载的。主要的原因是:安全,自定义的类加载器是JAVA的,可以监控加载过程,进而注入恶意代码。