java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
查了一些资料也不是太明白两个的区别,但是前者是最安全的用法
打个简单的比方,你一个WEB程序,发布到Tomcat里面运行。
首先是执行Tomcat org.apache.catalina.startup.Bootstrap类,这时候的类加载器是ClassLoader.getSystemClassLoader()。
而我们后面的WEB程序,里面的jar、resources都是由Tomcat内部来加载的,所以你在代码中动态加载jar、资源文件的时候,首先应该是使用Thread.currentThread().getContextClassLoader()。如果你使用Test.class.getClassLoader(),可能会导致和当前线程所运行的类加载器不一致(因为Java天生的多线程)。
Test.class.getClassLoader()一般用在getResource,因为你想要获取某个资源文件的时候,这个资源文件的位置是相对固定的。
java的类加载机制(jvm规范)是委托模型,简单的说,如果一个类加载器想要加载一个类,首先它会委托给它的parent去加载,如果它的所有parent都没有成功的加载那么它才会自己亲自来,有点儿像儿子使唤老子的感觉。
如果你使用Test.class.getClassLoader(),可能会导致和当前线程所运行的类加载器不一致 :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.
java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别的更多相关文章
- Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行.首先是执行Tomcat org.apache.catalina.startup ...
- Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
Thread.currentThread().getContextClassLoader() and Class.getClassLoader() 一.同一工程中: String path = T ...
- Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream
Class.forName() 和 ClassLoader.loadClass()的区别? Class.forName() 和 Class.forName().NewInstance()的区别? Cl ...
- Thread.currentThread().getContextClassLoader().getResourceAsStream
Thread.currentThread().getContextClassLoader().getResourceAsStream 2014年04月02日 06:49:47 OkidoGreen 阅 ...
- Thread.currentThread().getContextClassLoader().loadClass(className)和Class.forName(className)的区别
一.正文: 有去看开源框架的童鞋,应该会经常看到如下代码:Thread.currentThread().getContextClassLoader().loadClass(className),那这个 ...
- String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) 获取项目的绝对路径(shiro项目中来的八)
一,上代码 String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) file: ...
- JAVA 笔记 ClassLoader.getResourceAsStream() 与 Class.getResourceAsStream()的区别
Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致. 例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.c ...
- Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???
最近在看Java多线程这本书,但是发现里面有个概念自己搞不清楚.就是Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区 ...
- Thread.currentThread().getName() 和 this.getName()区别详解
currentThread的详解 currentThread方法是Thread类的一个静态方法,用来获取当前运行的代码段,正在被哪个线程调用.我们先来看一眼源码. 是一个native方法.直接与系统层 ...
随机推荐
- (转)C++——std::string类的引用计数
1.概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功课,于是你把自己关在房间里 ...
- 使用Azure Policy(策略)强制实现资源Tag的坑
Azure的Tag(标记)可以帮助运维人员对云资源分类从而方便地进行计费和资源管理.然而在具体实践中工程师部署云资源的时候常常会忘记给资源做标记打Tag. 针对这个问题,Azure的官方文档建议是可以 ...
- JMS 之 Active MQ 消息存储
一.消息的存储方式 ActiveMQ支持JMS规范中的持久化消息与非持久化消息 持久化消息通常用于不管是否消费者在线,它们都会保证消息会被消费者消费.当消息被确认消费后,会从存储中删除 非持久化消息通 ...
- 开始学习机器学习,从Ng的视频开始
时隔开5个月,忙完了考研和毕设后终于有时间搞自己想搞得,研究生导师方向是图像处理与机器学习结合,重新开工 何为机器学习? 对于机器学习(Machine Learning)的定义大体上有两种,第一种是美 ...
- Python数据类型及其方法详解
Python数据类型及其方法详解 我们在学习编程语言的时候,都会遇到数据类型,这种看着很基础也不显眼的东西,却是很重要,本文介绍了python的数据类型,并就每种数据类型的方法作出了详细的描述,可供知 ...
- 推荐几个Dynamic Crm的大神博客
ghostbear的博客:http://blog.csdn.net/ghostbear/article/category/1072859 ghostbear大神的博客是新手学习Dynamics Crm ...
- 中学之Vim实践课程
今天转发娄老师的一篇VIM编辑器的文章,很赞哦!(值得收藏)文后的参考资料记得看一看,也很棒! 原文地址:http://www.cnblog ...
- 【解决】VS2013 + Qt 5.7(5.6适用)使用QSqlDatabase出现“无法解析的外部符号"错误
原始日期: 2016-08-03 22:09 错误如下: error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __thiscal ...
- js返回顶部封装 简洁
js返回顶部封装 简洁: 加入html页面body最后面即可. <script> a(); function a() { $(function() { if ($(".j-to- ...
- linux下vim 查找命令
在命令模式下输入/word 这个是查找文件中“word”这个单词,是从文件上面到下面查找?word 这个是查找文件中“word”这个单词,是从文件下上面到面查找