http://www.jdon.com/jive/thread.jsp?forum=16&thread=10291
当然这是JBoss的特殊ClassLoader策略,Weblogic还不一样。
当然这是JBoss的特殊ClassLoader策略,Weblogic还不一样。
其实极限考验是在应用上,也如你说:
“ClassLoader设计时就应该考虑好的规则,这些才是做ClassLoader的重点”
如果整个系统从J2SE处都是你设计的,那么你可以考虑好整个规则,但是如果你在别人的classLoader框架上再做classloader,那么你必须了解别人的classloader规则,而且还要考虑移植性。这就是极限考验了。
According to javadoc,
Class.forName(String) is " equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class. " And obviously sometimes it is not equivalent to the context class loader of the current thread.
According to following URL: http://java.sun.com/developer/technicalArticles/Networking/classloaders/
"A thread's context class loader is, by default, set to the context class loader of the thread's parent. The hierarchy of threads is rooted at the primordial thread (the one that runs the program). The context class loader of the primordial thread is set to the class loader that loaded the application. The thread's context class loader will be the application's class loader. This loader is used by the Java runtime in the Java Remote Method Invocation (RMI) to load classes on behalf of the user application."
My feeling is that a lot of designs make use of this charactor of "context class loader" to pass a special classloader to all of the child processes. But "context class loader" is not designed for this, it primarily enables a class loaded from network to be able to load classes using the application's classloader instead of the network classloader which may not be a child of the applications's class loader.
So we should avoid using context class loader.
According to javadoc,
Class.forName(String) is " equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class. " And obviously sometimes it is not equivalent to the context class loader of the current thread.
According to following URL: http://java.sun.com/developer/technicalArticles/Networking/classloaders/
"A thread's context class loader is, by default, set to the context class loader of the thread's parent. The hierarchy of threads is rooted at the primordial thread (the one that runs the program). The context class loader of the primordial thread is set to the class loader that loaded the application. The thread's context class loader will be the application's class loader. This loader is used by the Java runtime in the Java Remote Method Invocation (RMI) to load classes on behalf of the user application."
My feeling is that a lot of designs make use of this charactor of "context class loader" to pass a special classloader to all of the child processes. But "context class loader" is not designed for this, it primarily enables a class loaded from network to be able to load classes using the application's classloader instead of the network classloader which may not be a child of the applications's class loader.
So we should avoid using context class loader.
解决方案参考该网址:
http://software.ccidnet.com/pub/disp/Article?articleID=11340&columnID=322
Thread.currentThread().getContextClassLoader())
返回当前线程使用的ClassLoader(2)。
如果ClassLoader(1)“等于”ClassLoader(2),那么这里的使用Class.forName(String clz)和ClassLoader(2).loadClass(String clz)就是等价的。
如果ClassLoader(1)“不等于”ClassLoader(2),那么就是不等价的。
在框架代码中,实际情况是ClassLoader(1)往往“不等于”ClassLoader(2):
即调用Class.forName(String clz)的语句所在的类的装载器不一定就是当前线程使用的类装载器。
因为:一个类装载完了在运行了,该类的所属的类装载器就确定了(不变),而在程序运行之中,线程
的类装载器都随时可以通过hread.currentThread().setContextClassLoader(loader)进行改变(变)。
==>一个不变,一个在变,当然就有可能不同了,所以banq说的对!
wangzhiliangofcn@yahoo.com.cn
wangzhiliang 的原因也一语中的。
Take Control of Class Loading in Java :
http://www.theserverside.com/news/thread.tss?thread_id=40763
作者:Binildas Christudas 01/26/2005
翻译:purplerain
http://kb.csdn.net/java/Articles/200510/a1843d60-05b1-456f-9f72-811cb45ea4ae.html