ClassLoader类加载器

package java.lang;
public abstract class ClassLoader {}
  • A class loader is an object that is responsible for loading classes. The
  • class ClassLoader is an abstract class.

一个类加载器是什么?顾名思义,是一个用来加载类的对象。ClassLoader 是一个抽象类。

  • Given the binary name of a class, a class loader should attempt to
  • locate or generate data that constitutes a definition for the class.

给一个二进制的类名,类加载器可以根据这个类名获取到对应类的组成信息。意思就是,经过类加载器的一番劳作,我们只需要给出一个类名,就能得到类的组成信息。

  • A typical strategy is to transform the name into a file name and then read a
  • “class file” of that name from a file system.

一个比较典型的策略就是,将类名类文件名做个映射,这样给出一个类名,类加载器直接去读取对应文件名的类文件即可。

  • Every Class object contains a Class#getClassLoader() reference to the ClassLoader that defined it.

每一个Class类,都含有一个getClassLoader() 方法,可以获取到加载这个类的类加载器。

在这里插入图片描述
是的没错,确实是有一个名叫Class的类。
在这里插入图片描述
Class的构造函数是私有的,只有Java虚拟机可以生成Class类。
在这里插入图片描述

  • Class objects for array classes are not created by class

  • loaders, but are created automatically as required by the Java runtime.

数组类里的多个类对象,并不是由多个类加载器创造的,而是在java运行环境自动创造的。

  • The class loader for an array class, as returned by {@link

  • Class#getClassLoader()} is the same as the class loader for its element

  • type;

    这个数组类的类加载器,跟它里面的对象的类加载器相同。

  • if the element type is a primitive type, then the array class has no

  • class loader.

如果里面的对象类是一个原始类,那么这个数组类将没有类加载器。

  • Applications implement subclasses of ClassLoader in order to
  • extend the manner in which the Java virtual machine dynamically loads
  • classes.

ClassLoader类其实是一个抽象类,我们会写一些ClassLoader的实现类是为了扩展虚拟机动态加载的机制,为其添加一些其他功能。

  • Class loaders may typically be used by security managers to indicate
  • security domains.

类加载器常常被用作安全域管理方面的作用。

  • The ClassLoader class uses a delegation model to search for
  • classes and resources.

ClassLoader类使用代理模式去查找类与资源。

  • Each instance of ClassLoader has an associated parent class loader.

每一个ClassLoader有一个关联的父类加载器。

  • When requested to find a class or resource, a ClassLoader instance will delegate the search for the class or resource to its parent class loader before attempting to find the class or resource itself.

当要查找一个类或者一个资源,类加载器会让父类加载器先去查找加载,而不是自己先去加载。这就是所谓的双亲委派机制。

  • The virtual machine’s built-in class loader, called the “bootstrap class loader”, does not itself have a parent but may serve as the parent of a ClassLoader instance.

虚拟机内置的类加载器叫做"bootstrap class loader",它没有父加载器,是初始的类加载器,可以作为别的类加载器的父亲。

  • Class loaders that support concurrent loading of classes are known as
  • parallel capable class loaders and are required to register
  • themselves at their class initialization time by invoking the
  • ClassLoaderClassLoader.registerAsParallelCapable
  • method. Note that the ClassLoader class is registered as parallel
  • capable by default. However, its subclasses still need to register themselves
  • if they are parallel capable.

类加载器如果想要获得并发加载类的能力,那么就需要在初始化的时候,调用自身的registerAsParallelCapable方法,将自己注册为并发型类加载器。需要注意的是,如果一个类加载器已经是被注册成拥有并发能力的,但是它的子类并不继承这一属性,子类还是需要自身调用一次进行注册。

  • In environments in which the delegation model is not strictly
  • hierarchical, class loaders need to be parallel capable, otherwise class
  • loading can lead to deadlocks because the loader lock is held for the
  • duration of the class loading process (see
  • loadClassmethods).

如果代理模式没有被很好的分级,类加载器就必须被注册成并发型的,否则会导致死锁。因为类加载器这个对象在加载类的过程中,被锁住了。

  • Normally, the Java virtual machine loads classes from the local file
  • system in a platform-dependent manner. For example, on UNIX systems, the
  • virtual machine loads classes from the directory defined by the
  • CLASSPATH environment variable.

一般来说,虚拟机加载加载本地文件会用平台自身的方式。比如,在UNIX系统,虚拟机会从CLASSPATH这个变量定义的文件夹下,加载文件。

  • However, some classes may not originate from a file; they may originate
  • from other sources, such as the network, or they could be constructed by an
  • application.

然而,类可能并不是都从一个本地文件得到的,它们可能从别的资源得到的,比如从网络,比如是被应用实时构造出来的。

  • The method defineClass(String, byte[], int, int) converts an array of bytes into an instance of class Class. Instances of this newly defined class can be created using Class.newInstance.

defineClass这个方法呢,可以将字符串转换成Class类,用这个类,可以生成Class实例。

  • 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 loadClass} method of
  • the class loader that originally created the class.

类构造器创造的类,可能会引用其他的类。为了确认引用的类,虚拟机调用最初始的类构造器的loadClass方法。

  • For example, an application could create a network class loader to
  • download class files from a server. Sample code might look like:
  • ClassLoader loader= new NetworkClassLoader(host,port);
  • Object main= loader.loadClass(“Main”, true).newInstance();

比如,一个程序需要构造一个从网络加载类的类加载器。示例代码可以如下

ClassLoader loader= new NetworkClassLoader(host,port);
Object main= loader.loadClass("Main", true).newInstance();
  • The network class loader subclass must define the methods findClass and loadClassData to load a class from the network.
  • Once it has downloaded the bytes that make up the class, it should use the method defineClass tocreate a class instance. A sample implementation is:

从网络上获取类的类加载器必须实现findClassloadClassData这两个方法。
当它从网络上下载好类的字符数据,就使用defineClass方法,去创造出这个类。
例子如下:


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

  • Any class name provided as a String parameter to methods in
  • ClassLoader must be a binary name as defined by
  • The Java™ Language Specification.
  • Examples of valid class names include:

一个类名必须是一个有效的字符串参数。例子如下:

  • “java.lang.String”
  • “javax.swing.JSpinner$DefaultEditor”
  • “java.security.KeyStore B u i l d e r Builder BuilderFileBuilder$1”
  • “java.net.URLClassLoader$3$1”

至此,我们大致理解了ClassLoader类是干什么了的,有什么作用:主要是进行类加载的,还可以并发加载类,以及类加载器的继承关系,双亲委派机制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rgbhi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值