您的位置:首页 > 其它

区别:Thread.currentThread().getContextClassLoader() and Class.getClassLoader()

2011-12-02 00:55 796 查看

What is different between Thread.currentThread().getContextClassLoader() and Class.getClassLoader()?

From API document, the
Thread.currentThread().getContextClassLoader()
returns the context
ClassLoader
for this Thread. The context
ClassLoader
is provided by the creator of the thread for use by code running in this thread when loading classes and resources. The default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application. The context
ClassLoader
can be set when a thread is created, and allows the creator of the thread to provide the appropriate class loader to code running in the thread when loading classes and resources. For example, JNDI and JAXP used thread's ClassLoader. You had better to use thread's ClassLoader in your own code when your code need to deployed on J2EE container.
The
Class.getClassLoader()
returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader. For example,
Class.getResource()
and
Class.forName()
will use the class loader of trhe current caller's class.
The Understanding Class.forName() by Ted Neward is excellent paper on ClassLoader.
=======================================================================

查找路径路径不同

1、从classpath根开始查找:Thread.currentThread().getContextClassLoader().getResource()

从当前类的目录下开始查找,要找根路径下的内容,必须以/为开头:

ConfigManager.class.getResource("/biz/unicorn-client.properties");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐