Java ClassLoader混乱 [英] Java ClassLoader Confusion

查看:65
本文介绍了Java ClassLoader混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某处读到,如果 Class A ClassLoaderA 加载,则所有类 A 取决于将由 ClassLoaderA 加载。是真的吗如果是真的,那么为什么我们需要设置上下文类加载器?像这样的一个: http://blog.markturansky.com/archives/21 ,谢谢。

I have read somewhere that if class A is loaded by ClassLoaderA then all the classes A depends on will be loaded by ClassLoaderA. Is it true? If it is true, then why we need to set the context class loader? like this one: http://blog.markturansky.com/archives/21, thanks in advance.

推荐答案

Javadocs对类加载器

The Javadocs say the following about the class loaders:


ClassLoader类使用委托模型搜索类
和资源。每个ClassLoader实例都有一个关联的父
类加载器。当请求查找类或资源时,
ClassLoader实例会将对类或
资源的搜索委托给其父类加载器,然后再尝试查找
类或资源本身。虚拟机的内置类加载器
称为引导类加载器,它本身没有父级,但是
可以充当ClassLoader实例的父级。

The ClassLoader class uses a delegation model to search for classes and resources. Each instance of ClassLoader has an associated parent class loader. 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.

因此,您的问题中的陈述显然是错误的。

As such, the statement in your question is apparently wrong.

在某些情况下,您可能希望创建隔离的类加载器,即不与应用程序类加载器链绑定的类加载器。在这些情况下,您可以创建一个父级为null的类加载器。 (即检查 URLClassLoader类中的构造函数)

There are cases in which you might like to create an isolated class loader, that is, a class loader not tied to the application class loader chain. In those cases you could create a class loader whose parent is null. (i.e. check constructors in URLClassLoader class)

这意味着,如果该隔离的类加载器无法找到/加载给定的类,它将立即失败,并显示 ClassNotFoundException 。这也意味着您可以拥有两个相互独立的类加载器层次结构,而不相互干扰。

This means that if this isolated class loader cannot find/load a given class it will immediately fail with a ClassNotFoundException. This means also that you can have two independent hierarchies of class loaders not interfering with each other.

-EDIT-

我将举例说明我的答案。假设:

I am going to illustrate my answer. Assume that:


  • 我有一个名为 Creature 的接口,位于一个名为 parent.jar

  • 我有一个名为 Jedi 的类,位于名为 child.jar 的JAR文件。

  • Jedi 类实现了该接口 Creature

  • I have an interface called Creature located in a JAR file called parent.jar.
  • I have an class called Jedi located in a JAR file called child.jar.
  • The class Jedi implements the interface Creature.

现在,让我们创建一个类加载器链,独立于

Now, let's create a chain of class loaders, independent of the current system chain.

ClassLoader parent = new URLClassLoader(new URL[]{parentJar.toURL()}, null);
ClassLoader child = new URLClassLoader(new URL[]{childJar.toURL()}, parent);

现在,我们可以要求子类加载器加载 Jedi

Now, we can ask the child class loader to load the class Jedi.

Class<?> klass1 = child.loadClass("com.star.wars.Jedi");

现在,如果查询其类加载器,您将看到它是由子类加载器加载的。到现在为止还挺好。但是它实现的接口呢?这取决于它,但是 Creature 接口仅对其父类加载器可用,对吧?

Now, if you query its class loader, you will see that it was loaded by the child class loader. So far so good. But what about the interface it implements. It depends on it, but the Creature interface was only available to its parent class loader, right?

嗯,如果您请求 klass1.getInterfaces()[0] .getClassLoader(),您会注意到它是父类加载器。

Well, if you request the klass1.getInterfaces()[0].getClassLoader() you will notice that it is the parent class loader.

这证明了该语句:


如果类A由ClassLoader A加载,则所有类A都取决于
将由ClassLoader A加载。

if class A is loaded by ClassLoader A then all the classes A depends on will be loaded by ClassLoader A.

完全是错误的。

这篇关于Java ClassLoader混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆