Java类加载器委派模型是强制性的吗? [英] Is Java Classloader Delegation model mandatory?

查看:112
本文介绍了Java类加载器委派模型是强制性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个自定义类加载器,而不是首先委托给它的父级,尝试搜索并加载该类本身是违反某些陈述/未声明的规则吗?

If i have a custom classloader which, instead of delegating to its parent first, tries a search and load of the class itself will it be a violation of some stated/unstated rule?

推荐答案

Tomcat webapp类加载器遵循这个模型,所以我想它至少可以一些范围:)来自 Tomcat类加载器文档

The Tomcat webapp classloader follows this model, so I imagine it works to at least some extent :) From the Tomcat classloader documentation:

如上所述,Web应用程序类加载器与默认的Java 2委派模型不同(根据Servlet规范2.4版,第9.7.2节Web应用程序类加载器中的建议)。当处理从Web应用程序的WebappX类加载器加载类的请求时,此类加载器将首先查看本地存储库,而不是在查找之前委派。有例外。作为JRE基类的一部分的类不能被覆盖。对于某些类(例如J2SE 1.4+中的XML解析器组件),可以使用J2SE 1.4认可的功能。最后,类加载器将显式忽略包含Servlet API类的任何JAR文件 - 不要在Web应用程序中包含此类JAR。 Tomcat 6中的所有其他类加载器都遵循通常的委托模式。

As mentioned above, the web application class loader diverges from the default Java 2 delegation model (in accordance with the recommendations in the Servlet Specification, version 2.4, section 9.7.2 Web Application Classloader). When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories first, instead of delegating before looking. There are exceptions. Classes which are part of the JRE base classes cannot be overriden. For some classes (such as the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be used. Last, any JAR file that contains Servlet API classes will be explicitly ignored by the classloader — Do not include such JARs in your web application. All other class loaders in Tomcat 6 follow the usual delegation pattern.

听起来我觉得你在做这个之前应该非常谨慎,但在某些情况下,如果小心使用,可能有用。您应该对其进行彻底记录(特别是如果第三方开发人员会看到它),并确保包含足够的日志记录/诊断信息以帮助解决棘手的问题。

It sounds to me like you should be very cautious before you do this, but in certain cases it could be useful, if used carefully. You should document it thoroughly (particularly if this will be seen by third party developers) and make sure you include enough logging/diagnostics to help troubleshoot thorny problems.

这篇关于Java类加载器委派模型是强制性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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