ContextClassLoader深度讲解
来源:互联网 发布:linux sed 编辑:程序博客网 时间:2024/06/07 15:10
Thread.currentThread().getContextClassLoader();
- 从方法名字来看,应该是获取当前上下文的类加载器
搞清楚这个问题, 当你在出现资源加载不到的时候就很容器解决
那么问题来了,为什么要这样设计? 解决了什么样的设计问题? 解决了什么样的开发问题? 我们带着这些问题,听小编娓娓道来
- 解决委派双亲加载模式的缺点
- 实现了JNDI等
- 解决开发中,文件加载不到的异常
Thread.currentThread().getContextClassLoader();this.getClass().getClassLoader();
类加载器之前一直迷惑,终于这个问题在一篇博客的回答中,找到了清晰易懂的解释
原文是这样的:
Thread context class loader存在的目的主要是为了解决parent delegation机制下无法干净的解决的问题。假如有下述委派链:
ClassLoader A -> System class loader -> Extension class loader -> Bootstrap class loader
那么委派链左边的ClassLoader就可以很自然的使用右边的ClassLoader所加载的类。
但如果情况要反过来,是右边的ClassLoader所加载的代码需要反过来去找委派链靠左边的ClassLoader去加载东西怎么办呢?没辙,parent delegation是单向的,没办法反过来从右边找左边.*
类加载器的委派双亲模式?
不明白的同学,赶紧补习一波.
就是说当我们this.getClass().getClassLoader();可以获取到所有已经加载过的文件,
但是System class loader -> Extension class loader -> Bootstrap class loader
就获取不到ClassLoader A
能加载到的信息,那么怎么办呢? 于是,Thread就把当前的类加载器,给保存下来了,其他加载器,需要的时候,就把当前线程的加载器,获取到.
那么什么场景下,会遇到这种情况那,当通常发生在有些JVM核心代码必须动态加载由应用程序开发人员提供的资源时eg:
- JNDI
JNDI(Java Naming and Directory Interface,Java命名和目录接口)是SUN公司提供的一种标准的Java命名系统接口,JNDI提供统一的客户端API,通过不同的访问提供者接口
在系统中,要调用开发者的资源,此时就遇到了这种情况
- JAX和rt.jar 因为是两个加载器加载的 那么BootStrap需要加载Ext的资源,怎么办? 这不是与委托机制相反了吗? 所以就不能只依赖委派双亲模式,那么怎么做
然后我们看一波,Thread源码的注释,提供了,获取上下文加载器方法Thread.currentThread().getContextClassLoader()
Thread
/* The context ClassLoader for this thread */ private ClassLoader contextClassLoader;
问题:
项目中需要加载应用配置,加载不到,需要用ClassPathResource
问题同上,父加载器要加载应用配置,因此需要调用上下文加载器
代码块分析
//获取文件绝对地址,并不是jar里面的文件路径URL resource = BlmSignature.class.getClassLoader().getResource(keystoreFilePath);String path = resource.getPath();//当发布到线上只发布jar文件, 所以就会报异常,找不到FileInputStream keystoreinputStream=new FileInputStream(path);
//正确的做法是,获取到jar包里面的文件,需要注意类加载是否能加载到的问题,//1. Spring工具keystorePath = "classpath:"+keystoreFilePath;ClassPathResource classPathResource = new ClassPathResource(keystorePath);InputStream keystoreinputStream = classPathResource.getInputStream();//使用类加载器加载classpath里面的//指定Thread.currentThread().getContextClassLoader();加载器SmileClassPathResource smileClassPathResource = new SmileClassPathResource(keystoreFilePath);InputStream keystoreinputStream=smileClassPathResource.getInputStream();
SmileClassPathResource源码
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;/** * @Package: org.smileframework.tool.io * @Description: 加载配置文件 * @author: liuxin * @date: 2017/12/19 上午9:23 */public class SmileClassPathResource { private final String path; private ClassLoader classLoader; public SmileClassPathResource(String name) { this(name, getDefaultClassLoader()); } public SmileClassPathResource(String name, ClassLoader classLoader) { this.path = name; this.classLoader = classLoader; } public static ClassLoader getDefaultClassLoader() { ClassLoader cl = null; try { cl = Thread.currentThread().getContextClassLoader(); } catch (Throwable var3) { ; } if(cl == null) { cl = ClassUtils.class.getClassLoader(); if(cl == null) { try { cl = ClassLoader.getSystemClassLoader(); } catch (Throwable var2) { ; } } } return cl; } public InputStream getInputStream() { InputStream is; if (this.classLoader != null) { is = this.classLoader.getResourceAsStream(this.path); } else {//当还是加载不到,调用上层加载器 is = ClassLoader.getSystemResourceAsStream(this.path); } if (is == null) { throw new RuntimeException(path + " cannot be opened because it does not exist"); } else { return is; } } public String getResourceStreamAsString() { InputStream is = getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } public static void main(String[] args) { SmileClassPathResource classPathResource = new SmileClassPathResource("logback.xml"); System.out.println(classPathResource.getResourceStreamAsString()); }}
- ContextClassLoader深度讲解
- ContextClassLoader
- contextclassloader 意义
- ContextClassLoader浅析
- contextClassLoader问题
- contextclassLoader--JDBC
- ContextClassLoader介绍
- ContextClassLoader浅析
- CreateIoCompletionPort的深度讲解
- android EventBus深度讲解
- 博弈论深度讲解
- 快速幂取余深度讲解
- empty()函数深度讲解
- ContextClassLoader的意义
- java-ContextClassLoader的意义
- java输入输出流深度讲解
- Kafka深度讲解+Zookeeper存储
- java算法视频深度讲解
- LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Latex 如何画出决策树示意图
- 转载: 德鲁克:管理自己
- 深入理解Java虚拟机 ch7 虚拟机加载机制 读书笔记
- pycharm 快捷键
- ContextClassLoader深度讲解
- 冒泡排序及优化
- 卡尔曼滤波简单理解及C语言代码
- PAT——A+B和C
- Font Awesome(十六)
- Codeforces Round #453 (Div. 2)
- 30岁的感悟
- 拍照扫描车牌号识别的一种技术sdk
- 关于C++中的常量引用和常量成员函数