问题描述
如果我有一个自定义类加载器,而不是首先委托给它的父级,尝试搜索并加载该类本身是违反某些陈述/未声明的规则吗?
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类加载器遵循这个模型,所以我想它至少可以一些范围:)来自:
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中的所有其他类加载器都遵循通常的委托模式。
听起来我觉得你在做这个之前应该非常谨慎,但在某些情况下,如果小心使用,可能有用。您应该对其进行彻底记录(特别是如果第三方开发人员会看到它),并确保包含足够的日志记录/诊断信息以帮助解决棘手的问题。
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类加载器委派模型是强制性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!