java 自定义类加载器有什么用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10828863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:45:35  来源:igfitidea点击:

What is the use of Custom Class Loader

javaclassloader

提问by Priyank Doshi

Recently I came accross the java custom class loader api. I found one use over here, kamranzafar's blogI am a bit new to the class loader concept. Can any one explain in detail, what are the different scenarios where we may need it or we should use it?

最近我遇到了 java 自定义类加载器 api。我在这里找到了一个用途,kamranzafar 的博客我对类加载器的概念有点陌生。谁能详细解释一下,我们可能需要或应该使用它的不同场景是什么?

回答by Ramesh PVK

Custom class loaders are useful in larger architectures consisting of several module/applications. Here are the advantages of the custom class loader:

自定义类加载器在由多个模块/应用程序组成的大型体系结构中很有用。以下是自定义类加载器的优点:

  • Provides Modular architectureAllows to define multiple class loader allowing modular architecture.
  • Avoiding conflictsClearly defines the scope of the class to within the class loader.
  • Support VersioningSupports different versions of class within same VM for different modules.
  • Better Memory ManagementUnused modules can be removed which unloads the classes used by that module, which cleans up memory.
  • Load classes from anywhereClasses can be loaded from anywhere, for ex, Database, Networks, or even define it on the fly.
  • Add resources or classes dynamicallyAll the above features allows you add classes or resources dynamically.
  • Runtime Reloading Modified ClassesAllows you to reload a class or classes runtime by creating a child class loader to the actual class loader, which contains the modified classes.
  • 提供模块化架构允许定义多个允许模块化架构的类加载器。
  • 避免冲突在类加载器中明确定义类的范围。
  • 支持版本控制支持同一 VM 中不同模块的不同版本的类。
  • 更好的内存管理可以删除未使用的模块,从而卸载该模块使用的类,从而清理内存。
  • 从任何地方加载类 可以从任何地方加载类例如,数据库、网络,甚至可以动态定义它
  • 动态添加资源或类上述所有功能都允许您动态添加类或资源。
  • 运行时重新加载修改后的类允许您通过创建一个子类加载器到包含修改后的类的实际类加载器来重新加载一个或多个类运行时。

回答by Michael Wiles

The primary use is in Application servers so that they can run two applications and not have the classes conflict. i.e. if application 1 has a class with the same name as application 2, with a custom class loader application 1 will load its class and application 2 will load its class.

主要用途是在应用程序服务器中,以便它们可以运行两个应用程序并且不会发生类冲突。即,如果应用程序 1 有一个与应用程序 2 同名的类,使用自定义类加载器,应用程序 1 将加载其类,应用程序 2 将加载其类。

Also if a class is loaded by a custom class loader it is possible to unload that class from the JVM. Again useful in application servers.

此外,如果一个类是由自定义类加载器加载的,则可以从 JVM 中卸载该类。在应用服务器中再次有用。

Another use would be for instrumentation - One way of doing aspect oriented programming or when using some persistence API's. With a custom classloader you can add behaviour to the loaded classes before they are passed over to the running application.

另一种用途是用于检测 - 进行面向方面编程或使用某些持久性 API 时的一种方法。使用自定义类加载器,您可以在加载的类传递给正在运行的应用程序之前为其添加行为。

回答by mikera

Java class loaders do pretty much what the name suggests: load classes into memory so that they can be used.

Java 类加载器几乎顾名思义:将类加载到内存中以便可以使用它们。

Classes are also linked with the ClassLoader that loaded them.

类也与加载它们的类加载器相关联。

Custom class loaders therefore open up a variety of interesting possibilities:

因此,自定义类加载器开辟了各种有趣的可能性:

  • Loading multiple versions of the same class with different classloaders (e.g. to resolve possible versioning conficts for example)
  • Loading and unloading classes dynamically at runtime
  • Generating new classes (e.g. JVM languages like Clojure use various classloading tricks to generate new compiled classes to represent Clojure functions at runtime)
  • Loading classes from non-standard sources
  • 使用不同的类加载器加载同一类的多个版本(例如,解决可能的版本冲突)
  • 在运行时动态加载和卸载类
  • 生成新类(例如 Clojure 等 JVM 语言使用各种类加载技巧来生成新的编译类以在运行时表示 Clojure 函数)
  • 从非标准源加载类

Normal Java applications don't usually need to worry about classloaders. But if you are writing a framework or platform that needs to host other code then they become much more important / relevant.

普通的 Java 应用程序通常不需要担心类加载器。但是,如果您正在编写需要托管其他代码的框架或平台,那么它们就变得更加重要/相关。