简体   繁体   English

在Java中使用3个类加载器的原因是什么?

[英]What is the reason for having 3 Class loaders in Java

Java has 3 class loaders: Java有3个类加载器:

  • BootStrap, 自举,
  • Extension and 扩展和
  • System 系统

and they have one role; 他们有一个角色; loading classes from different packages. 从不同的包加载类。

But why does Java have 3 different class loaders instead of just one, since one class loader can load all the necessary classes? 但是为什么Java有3个不同的类加载器而不是只有一个,因为一个类加载器可以加载所有必需的类?

The reason for having the three basic class loaders (Bootstrap, extension, system) is mostly security. 拥有三个基本类加载器(Bootstrap,扩展,系统)的原因主要是安全性。

Prior to version 1.2 of the JVM, there was just one default class loader, which is what is currently called the "Bootstrap" class loader. 在JVM 1.2版之前,只有一个默认的类加载器,这就是当前所谓的“Bootstrap”类加载器。

The way classes are loaded by class loaders is that each class loader first calls its parent, and if that parent doesn't find the requested class, the current one is looking for it itself. 类加载器加载类的方式是每个类加载器首先调用其父类,如果该父类没有找到所请求的类,则当前的类正在查找它本身。

A key concept is the fact that the JVM will not grant package access (the access that methods and fields have if you didn't specifically mention private , public or protected ) unless the class that asks for this access comes from the same class loader that loaded the class it wishes to access. 一个关键的概念是JVM不会授予包访问权限(如果您没有特别提到privatepublicprotected方法和字段的访问权限),除非请求此访问的类来自同一个类加载器 ,加载了它希望访问的类。

So, suppose a user calls his class java.lang.MyClass . 因此,假设用户调用他的类java.lang.MyClass Theoretically, it could get package access to all the fields and methods in the java.lang package and change the way they work. 从理论上讲,它可以获得对java.lang包中所有字段和方法的包访问,并改变它们的工作方式。 The language itself doesn't prevent this. 语言本身并不能阻止这种情况。 But the JVM will block this, because all the real java.lang classes were loaded by bootstrap class loader. 但是JVM会阻止它,因为所有真正的java.lang类都是由bootstrap类加载器加载的。 Not the same loader = no access. 不一样的装载机=无法访问。

There are other security features built into the class loaders that make it hard to do certain types of hacking. 类加载器中内置了其他安全功能,这使得很难进行某些类型的黑客攻击。

So why three class loaders? 那么为什么三个类加载器呢? Because they represent three levels of trust. 因为它们代表三个层次的信任。 The classes that are most trusted are the core API classes. 最值得信任的类是核心API类。 Next are installed extensions, and then classes that appear in the classpath, which means they are local to your machine. 接下来是已安装的扩展,然后是类路径中显示的类,这意味着它们是您的计算机的本地。

For a more extended explanation, refer to Bill Venners's "Inside the Java Virtual Machine" . 有关更广泛的解释,请参阅Bill Venners的“Java虚拟机内部”

The main usage of class loaders is in application servers. 类加载器的主要用途是在应用程序服务器中。

You want to be able to start Tomcat (for example). 您希望能够启动Tomcat(例如)。 This already needs at least one classloader to run tomcat itself. 这已经需要至少一个类加载器来运行tomcat本身。

Then you want to be able to deploy an application into Tomcat. 然后,您希望能够将应用程序部署到Tomcat中。 So Tomcat itself needs to load an analyze the classes of the application, which didn't even exist when Tomcat was started. 因此Tomcat本身需要加载一个分析应用程序的类,当Tomcat启动时它们甚至都不存在。

Then you want to be able todeploy another application in Tomcat. 然后,您希望能够在Tomcat中部署另一个应用程序。 Maybe this second application uses a library that the first one also uses, but in a different version. 也许第二个应用程序使用的是第一个也使用的库,但是使用的是另一个版本。 So you want each app to have its own isolated class loader, otherwise the classes of app 2 might interfere badly with the classes from app 1. 所以你希望每个应用程序都有自己独立的类加载器,否则app 2的类可能会干扰app 1中的类。

Then you want to be able to undeploy one of the webapps. 然后,您希望能够取消部署其中一个Web应用程序。 So its class loader must be destroyed and garbage-collected, to avoid a huge memory leak. 因此必须销毁它的类加载器并进行垃圾收集,以避免巨大的内存泄漏。

There are of course many other usages, but that's the one that is the most commonly used (in my experience). 当然还有许多其他用法,但这是最常用的(根据我的经验)。

  1. Multiple class loaders are present to load multiple applications simultaneously(One to load Server & Other to deploy in the server). 存在多个类加载器以同时加载多个应用程序(一个加载服务器和其他以在服务器中部署)。
  2. Each loader has a hierarchy to load only certain classes to ensure security among them. 每个加载器都有一个层次结构,只加载某些类以确保它们之间的安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM