PMD rule “Use Proper Class Loader” explaination?

做~自己de王妃 提交于 2019-12-01 06:21:54

问题


There is the following PMD rule:

Use Proper Class Loader (Critical)

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

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


回答1:


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.

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.

(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).



来源:https://stackoverflow.com/questions/34787419/pmd-rule-use-proper-class-loader-explaination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!