栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础学习笔记之类加载器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础学习笔记之类加载器

类加载器

java类加载器就是在运行时在JVM中动态地加载所需的类,java类加载器基于三个机制:委托,可见,单一。

把classpath下的那些.class文件加载进内存,处理后成为字节码,这些工作是类加载器做的。

  1. 委托机制指的是将加载类的请求传递给父加载器,如果父加载器找不到或者不能加载这个类,那么再加载他。
  2. 可见性机制指的是父加载器加载的类都能被子加载器看见,但是子加载器加载的类父加载器是看不见的。
  3. 单一性机制指的是一个类只能被同一种加载器加载一次。

默认类加载器

系统默认三个类加载器:

  1. BootStrap
  2. ExtClassLoader
  3. AppClassLoader

类加载器也是java类,而BootStrap不是。 验证代码:

public class ClassLoaderTest {
  public static void main(String[] args) {
    System.out.println(System.class.getClassLoader());
  }
}

输出:null

如果使用System.out.println(System.class.getClassLoader().toString);,则报空指针异常:

Exception in thread "main" java.lang.NullPointerException
  at com.iot.classloader.ClassLoaderTest.main(ClassLoaderTest.java:10)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:483)
  at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

可见,System类是由BootStrap类加载器加载。

类加载器的委托机制

类加载器的树状图

类加载器

一般加载类的顺序:

  1. 首先当前线程的类加载器去加载线程中的第一个类
  2. 如果类A应用了类B,java虚拟机将使用加载类A的类加载器来加载类B
  3. 还可以直接调用ClassLoader.loadClass()方法来制定某个类加载器去加载某个类

自定义类加载器的编写原理

API:

Class ClassLoader

模板方法设计模式

父类:

loadClass(类加载的流程,模板)
findClass供子类覆盖的、被loadClass方法调用的类加载逻辑
defineClass得到class文件转换成字节码

子类:覆盖findClass方法

例子:

loadClass方法的源码

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;
  }
}

API文档中的例子:

class NetworkClassLoader extends ClassLoader {
   String host;
   int port;

   public Class findClass(String name) {
     byte[] b = loadClassData(name);
     return defineClass(name, b, 0, b.length);
   }

   private byte[] loadClassData(String name) {
     // load the class data from the connection
     . . .
   }
 }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/150572.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号