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

问题描述

有人能给我一个很好的资源或者解释一下Class Loaders背后的概念吗?我在类加载器上找到了以下资源:但仍然没有帮助。以下问题可能看起来很愚蠢,但试图回答它们总是让我感到困惑。

Can anyone point me a good resource or explain me about the concept behind Class Loaders? I found the following resource on class loaders http://www.onjava.com/lpt/a/5586 but still no help. The following questions may look silly but trying to answer them always confuses me.


  • 为什么开发人员编写自定义类加载器,为什么不调用Bootstrap类加载器调用自定义类?有什么需要定义自定义类加载器?

  • 为什么有这么多种类的加载器?例如:Bootsrap,Comman,Catalina类装载机等,

  • Why do developers write Custom class loaders, why not invoke a Bootstrap class loader to invoke your custom classes? What is the need to define custom class loaders?
  • Why there are so many varieties of class loaders? eg: Bootsrap, Comman, Catalina class loader etc.,

提前致谢。

推荐答案

我发现了以下有效理由来创建自定义类加载器:

I found the following, valid reasons to create custom classloaders:


  1. 您希望从非常规源加载类(例如,一个类的字节码存储在数据库中,通过网络或 - MessengerPidgeonClassLoader)。对于此类情况,API中已存在一些ClassLoader实现,例如。

  1. You want to load a class from an unconventional source (For example, the bytecode for one class is stored in a database, across the network or carried as 0 and 1s by pidgeons - MessengerPidgeonClassLoader). There is some ClassLoader implementations already in the API for such cases, such as URLClassLoader.

您需要定义不同的层次结构来加载类。 ClassLoader的默认实现首先将搜索委托给父级,然后他们尝试自己加载该类。也许你想要一个不同的等级。这就是为什么OSGI和Eclipse有自己的ClassLoaders作为Manifest .MF文件定义所有类型的奇怪层次路径(例如,伙伴类加载)的原因。所有Eclipse类加载器都实现了BundleClassLoader接口,并有一些额外的代码来查找Eclipse插件中的资源。

You need to define a different hierarchy to load classes. Default implementations of the ClassLoader delegate the search first to the parent, then they try to load the class themselves. Maybe you want a different hierarchy. This is the reason why OSGI and Eclipse have its own ClassLoaders as the Manifest .MF files define all types of weird hierarchy paths (buddy-classloading, for example). All Eclipse classloaders implement the BundleClassLoader interface and have some extra code to find resources within Eclipse Plugins.

您需要对字节码进行一些修改。也许字节码是加密的,你可以动态解密它()。也许你想要修补动态加载的类(一个JDO字节码增强)。

You need to do some modification to the bytecode. Maybe the bytecode is encrypted, and you will unencrypt it on the fly (Not that it helps, really, but has been tried). Maybe you want to "patch" the classes loaded on the fly (A la JDO bytecode enhancement).

使用如果您需要从内存中卸载类,或者加载类而不是在运行时更改其定义,则需要不同于System Classloader的类加载器。典型的情况是,例如,从XML文件动态生成类的应用程序,然后尝试重新加载此类。一旦类在System Classloader中,就无法卸载它并有一个新的定义。

Using a different classloader than the System Classloader is required if you need to unload classes from memory, or to load classes than could change their definition at runtime. A typical case is an application that generates a class on the fly from an XML file, for example, and then tries to reload this class. Once a class is in the System Classloader, there is no way to unload it and have a new definition.

这篇关于Java类加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 06:50