PMD规则“使用适当的类加载器”交代? [英] PMD rule "Use Proper Class Loader" explaination?

查看:148
本文介绍了PMD规则“使用适当的类加载器”交代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有以下PMD规则:


使用正确的类加载器(严重)

Use Proper Class Loader (Critical)

在J2EE中 getClassLoader()可能无法按预期工作。请改用 Thread.currentThread()。getContextClassLoader()

In J2EE getClassLoader() might not work as expected. Use Thread.currentThread().getContextClassLoader() instead.

可以稍微解释他们在想什么?这里究竟什么意思是J2EE环境?

Can somewhat explain more what they are thinking of? What exactly means "J2EE" environment here?

推荐答案

J2EE环境是指Java Enterprise Edition。在这里,您不要将程序作为JVM上的独立应用程序运行,而是让JEE应用程序服务器(例如Glassfish或Tomcat)为您运行它。

J2EE environment refers to the Java Enterprise Edition. Here, you do not run your program as a standalone application on the JVM, but instead, you let the JEE Application Server (e.g. Glassfish or Tomcat) to run it for you.

应用程序服务器提供了许多不同的方式来运行程序,例如它们提供并发和分布式执行。 AppServers使用类加载器,有时会有层次结构,例如EAR(Enterprise ARchive)有一个,EAR内部的WAR(Web ARchive)有一个不同的。因此,无法确保您始终使用 getClassLoader()调用获得正确的ClassLoader。使用 Thread.currentThread()。getContextClassLoader(),您将始终获得加载应用程序的ClassLoader。

Application Servers provide a lot of different ways to run your program, for example they offer concurrent and distributed execution. AppServers play with classloaders, sometimes there is a hierarchy, for example the EAR (Enterprise ARchive) has one, and a WAR (Web ARchive) inside the EAR has a different one. Because of this it is not ensured you will always get the right ClassLoader with a getClassLoader() call. With Thread.currentThread().getContextClassLoader() you will always get the ClassLoader that loaded your app.

(注意:这不完全正确,一些应用程序使用ContextClassLoader(如Spring,OSGi)制作技巧。但是,这是获得正确的ClassLoader的最佳方法) 。

(Note: This is not entirely true, some applications make trick with ContextClassLoader (like Spring, OSGi). Still, this is the best way to get your proper ClassLoader).

这篇关于PMD规则“使用适当的类加载器”交代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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