java - 为什么ClassLoader的缓存是按升序检查的?

标签 java classloader

为什么ClassLoader的缓存是按升序检查的,而类加载是按降序进行的? Class loaders delegation scheme

最佳答案

Java 中的类加载器基于三个原则工作:委托(delegate)、可见性和唯一性。委托(delegate)原则将类加载请求转发给父类加载器,如果父类加载器无法找到或加载类,则只加载该类。可见性原则是让子类加载器可以看到父类加载器加载的所有类,而父类加载器看不到子类加载器加载的类。唯一性原则允许只加载一次类,这基本上是通过委托(delegate)实现的,并确保子类加载器不会重新加载父类已加载的类。

换句话说就是描述的here :

The class loaders in Java are organized in a tree. By request a class loader determines if the class has already been loaded in the past, looking up in its own cache. If the class is present in the cache the CL returns the class, if not, it delegates the request to the parent. If the parent is not set (is Null) or can not load the class and throws a ClassNotFoundException the classloader tries to load the class itself and searches its own path for the class file. If the class can be loaded it is returned, otherwise a ClassNotFoundException is thrown. The cache lookup goes on recursively from child to parent, until the tree root is reached or a class is found in cache. If the root is reached the class loaders try to load the class and unfold the recursion from parent to child. Summarizing that we have following order:

  • Cache
    • Parent
    • Self

This mechanism ensures that classes tending to be loaded by class loaders nearest to the root.

关于java - 为什么ClassLoader的缓存是按升序检查的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34158114/

相关文章:

java - 无法在listview android中读取sqlite数据库

java - Java 中 ['_a-z0-9-\\+] 正则表达式的含义

java - 哪个类加载器加载了提供的实例的类

java - 有没有办法覆盖默认的 java 类加载器来记录它加载的每个类?

java - 在运行时将 jar 文件添加到类路径,无需反射技巧

java - 删除时 JPA "org.apache.openjpa.persistence.ArgumentException"

java - Spring AOP 创建额外的 bean

java - 类加载器并加载位置不等于其包的类

java反射: Is it possible to override a private method by change method Id?

java - 列表扩展的增量 future