PMD规则“使用适当的类加载器”解释?
有以下PMD规则:
使用正确的类加载器(严重)
在J2EE中,
getClassLoader()
可能无法按预期工作。 请改用Thread.currentThread().getContextClassLoader()
。
可以在某种程度上解释他们的想法吗? 究竟什么意思是“J2EE”环境?
J2EE环境是指Java Enterprise Edition。 在这里,您不要将程序作为JVM上的独立应用程序运行,而是让JEE Application Server(例如Glassfish或Tomcat)为您运行它。
应用程序服务器提供了许多不同的方法来运行程序,例如它们提供并发和分布式执行。 AppServers使用类加载器,有时会有层次结构,例如EAR(Enterprise ARchive)有一个,EAR内部的WAR(Web ARchive)有一个不同的。 因此,无法确保您始终使用getClassLoader()
调用获得正确的ClassLoader。 使用Thread.currentThread().getContextClassLoader()
您将始终获得加载应用程序的ClassLoader。
(注意:这不完全正确,一些应用程序使用ContextClassLoader(如Spring,OSGi)制作技巧。但是,这是获得正确的ClassLoader的最佳方法)。
- ClassLoader.getResources在Weblogic / Spring应用程序上返回一个空的Enumeration
- 为什么这段代码不会抛出NullPointerException
- 动态类重新加载仅在调试模式下工作,为什么/如何真正起作用?
- 自定义classLoader问题
- 我得到一个“线程中的exception”主“java.lang.NoClassDefFoundError:org / openqa / selenium / WebDriver”
- Java 7u55 Eclipse系统片段类加载器
- 如何包含同一依赖项的两个不同版本?
- 修补或覆盖核心Java 10类的实现
- 为什么ContextClassLoader返回带有感叹号字符的路径?