查了一些资料也不是太明白两个的区别,但是前者是最安全的用法

打个简单的比方,你一个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.

Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别的更多相关文章

  1. java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别

    查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行.首先是执行Tomcat org.apache.catalina.startup ...

  2. Thread.currentThread().getContextClassLoader() and Class.getClassLoader()

    Thread.currentThread().getContextClassLoader() and Class.getClassLoader()   一.同一工程中: String path = T ...

  3. Thread.currentThread().getContextClassLoader().loadClass(className)和Class.forName(className)的区别

    一.正文: 有去看开源框架的童鞋,应该会经常看到如下代码:Thread.currentThread().getContextClassLoader().loadClass(className),那这个 ...

  4. Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream

    Class.forName() 和 ClassLoader.loadClass()的区别? Class.forName() 和 Class.forName().NewInstance()的区别? Cl ...

  5. Thread.currentThread().getContextClassLoader().getResourceAsStream

    Thread.currentThread().getContextClassLoader().getResourceAsStream 2014年04月02日 06:49:47 OkidoGreen 阅 ...

  6. String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) 获取项目的绝对路径(shiro项目中来的八)

    一,上代码 String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) file: ...

  7. Thread.currentThread().getName() 和 this.getName()区别详解

    currentThread的详解 currentThread方法是Thread类的一个静态方法,用来获取当前运行的代码段,正在被哪个线程调用.我们先来看一眼源码. 是一个native方法.直接与系统层 ...

  8. 有关getClassLoader().getResourceAsStream(fileName)、class.getResourceAsStream(fileName)和().getContextClassLoader().getResourceAsStream(fileName)的区别

    一:前言 在自己获取属性时,碰见了XX.class.getResourceAsStream(fileName),自己对这个其实不是很理解,上网查了下资料,又看到了上述的几个,所以就研究了下. 二:内容 ...

  9. Thread.currentThread()和this的区别——《Java多线程编程核心技术》

    前言:在阅读<Java多线程编程核心技术>过程中,对书中程序代码Thread.currentThread()与this的区别有点混淆,这里记录下来,加深印象与理解. 具体代码如下: pub ...

随机推荐

  1. 打印iphone支持的所有字体

    //打印iphone支持的所有字体 NSArray *familyNames = [UIFont familyNames]; for(NSString *familyName in familyNam ...

  2. WC2018 即时战略

    交互题 一棵树,一开始只有 1 号点是已知的,其他的都是未知的,你可以调用函数 explore(x,y) ,其中 x 必须是已知的,函数会找到 x 到 y 路径上第二个点,并把它标成已知,求最小步数使 ...

  3. 转载:关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ

    转载: http://blog.csdn.net/konglongaa/article/details/52208273

  4. Storm实时计算:流操作入门编程实践

    转自:http://shiyanjun.cn/archives/977.html Storm实时计算:流操作入门编程实践   Storm是一个分布式是实时计算系统,它设计了一种对流和计算的抽象,概念比 ...

  5. Laser

    Petya is the most responsible worker in the Research Institute. So he was asked to make a very impor ...

  6. Oracle 12C 新特性之 db默认字符集AL32UTF8、PDB支持不同字符集

    一. db默认字符集AL32UTF8Specify the database character set when you create the database. Starting from Ora ...

  7. 伪元素after,before,css/js控制样式

    CSS<style> body { font: 200%/1.45 charter; } ref::before { content: '\00A7'; letter-spacing: . ...

  8. bzoj 2242 [SDOI2011]计算器——BSGS模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...

  9. BZOJ1999 树网的核[数据加强版]

    1999: [Noip2007]Core树网的核 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1214  Solved: 336[Submit][St ...

  10. unix下网络编程之I/O复用(四)

    首先需要了解的是select函数: select函数 #include<sys/select.h> #include<sys/time.h> int select (int m ...