在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

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

2016-8-29 13:18| 发布者: zhangjf| 查看: 741| 评论: 0

摘要: 类加载器 java类加载器就是在运行时在JVM中动态地加载所需的类,java类加载器基于三个机制:委托,可见,单一。 把classpath下的那些.class文件加载进内存,处理后成为字节码,这些工作是类加载器做的。 委托机 ...

类加载器

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

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

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

默认类加载器

系统默认三个类加载器:

  1. BootStrap
  2. ExtClassLoader
  3. AppClassLoader

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

  1. public class ClassLoaderTest {
  2. public static void main(String[] args) {
  3. System.out.println(System.class.getClassLoader());
  4. }
  5. }
复制代码

输出:null

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

  1. Exception in thread "main" java.lang.NullPointerException
  2. at com.iot.classloader.ClassLoaderTest.main(ClassLoaderTest.java:10)
  3. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  5. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  6. at java.lang.reflect.Method.invoke(Method.java:483)
  7. 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方法的源码

  1. protected Class<?> loadClass(String name, boolean resolve)
  2. throws ClassNotFoundException
  3. {
  4. synchronized (getClassLoadingLock(name)) {
  5. // First, check if the class has already been loaded
  6. Class<?> c = findLoadedClass(name);
  7. if (c == null) {
  8. long t0 = System.nanoTime();
  9. try {
  10. if (parent != null) {
  11. c = parent.loadClass(name, false);
  12. } else {
  13. c = findBootstrapClassOrNull(name);
  14. }
  15. } catch (ClassNotFoundException e) {
  16. // ClassNotFoundException thrown if class not found
  17. // from the non-null parent class loader
  18. }
  19. if (c == null) {
  20. // If still not found, then invoke findClass in order
  21. // to find the class.
  22. long t1 = System.nanoTime();
  23. c = findClass(name);
  24. // this is the defining class loader; record the stats
  25. sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
  26. sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
  27. sun.misc.PerfCounter.getFindClasses().increment();
  28. }
  29. }
  30. if (resolve) {
  31. resolveClass(c);
  32. }
  33. return c;
  34. }
  35. }
复制代码

API文档中的例子:

  1. class NetworkClassLoader extends ClassLoader {
  2. String host;
  3. int port;
  4. public Class findClass(String name) {
  5. byte[] b = loadClassData(name);
  6. return defineClass(name, b, 0, b.length);
  7. }
  8. private byte[] loadClassData(String name) {
  9. // load the class data from the connection
  10. . . .
  11. }
  12. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-6 19:54

Copyright 2015-2025 djqfx

返回顶部