gpt4 book ai didi

java - Java 类加载器委派模型是强制性的吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:15 24 4
gpt4 key购买 nike

如果我有一个自定义类加载器,而不是首先委托(delegate)给它的父类,而是尝试搜索和加载类本身,这是否违反了一些规定/未规定的规则?

最佳答案

Tomcat webapp 类加载器遵循这个模型,所以我想它至少在一些 范围内有效:) 来自the Tomcat classloader documentation :

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.

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

关于java - Java 类加载器委派模型是强制性的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10701385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com