深入理解ContextClassLoader加載器
ContextClassLoader
Thread.currentThread().getContextClassLoader(); - 從方法名字來看,應(yīng)該是獲取當(dāng)前上下文的類加載器 搞清楚這個(gè)問題, 當(dāng)你在出現(xiàn)資源加載不到的時(shí)候就很容器解決
那么問題來了,為什么要這樣設(shè)計(jì)? 解決了什么樣的設(shè)計(jì)問題? 解決了什么樣的開發(fā)問題?
- 解決委派雙親加載模式的缺點(diǎn)
- 實(shí)現(xiàn)了JNDI等
- 解決開發(fā)中,文件加載不到的異常
Thread.currentThread().getContextClassLoader(); this.getClass().getClassLoader();
類加載器之前一直迷惑,終于這個(gè)問題在一篇博客的回答中,找到了清晰易懂的解釋
原文是這樣的:
Thread context class loader存在的目的主要是為了解決parent delegation機(jī)制下無法干凈的解決的問題。
假如有下述委派鏈: ClassLoader A -> System class loader -> Extension class loader -> Bootstrap class loader
那么委派鏈左邊的ClassLoader就可以很自然的使用右邊的ClassLoader所加載的類。
但如果情況要反過來,是右邊的ClassLoader所加載的代碼需要反過來去找委派鏈靠左邊的ClassLoader去加載東西怎么辦呢?沒轍,parent delegation是單向的,沒辦法反過來從右邊找左邊.*
類加載器的委派雙親模式?
就是說當(dāng)我們this.getClass().getClassLoader();可以獲取到所有已經(jīng)加載過的文件, 但是 System class loader -> Extension class loader -> Bootstrap class loader 就獲取不到 ClassLoader A 能加載到的信息,那么怎么辦呢? 于是,Thread就把當(dāng)前的類加載器,給保存下來了,其他加載器,需要的時(shí)候,就把當(dāng)前線程的加載器,獲取到.
那么什么場(chǎng)景下,會(huì)遇到這種情況那,當(dāng)通常發(fā)生在有些JVM核心代碼必須動(dòng)態(tài)加載由應(yīng)用程序開發(fā)人員提供的資源時(shí)eg:
JNDI
JNDI(Java Naming and Directory Interface,Java命名和目錄接口)是SUN公司提供的一種標(biāo)準(zhǔn)的Java命名系統(tǒng)接口,JNDI提供統(tǒng)一的客戶端API,通過不同的訪問提供者接口
在系統(tǒng)中,要調(diào)用開發(fā)者的資源,此時(shí)就遇到了這種情況
JAX和rt.jar 因?yàn)槭莾蓚€(gè)加載器加載的 那么BootStrap需要加載Ext的資源,怎么辦? 這不是與委托機(jī)制相反了嗎? 所以就不能只依賴委派雙親模式,那么怎么做
然后我們看一波,Thread源碼的注釋,提供了,獲取上下文加載器方法 Thread.currentThread().getContextClassLoader()
Thread
/* The context ClassLoader for this thread */
private ClassLoader contextClassLoader;問題:
項(xiàng)目中需要加載應(yīng)用配置,加載不到,需要用ClassPathResource
問題同上,父加載器要加載應(yīng)用配置,因此需要調(diào)用上下文加載器
代碼塊分析
//獲取文件絕對(duì)地址,并不是jar里面的文件路徑 URL resource = BlmSignature.class.getClassLoader().getResource(keystoreFilePath); String path = resource.getPath(); //當(dāng)發(fā)布到線上只發(fā)布jar文件, 所以就會(huì)報(bào)異常,找不到 FileInputStream keystoreinputStream=new FileInputStream(path);
//正確的做法是,獲取到j(luò)ar包里面的文件,需要注意類加載是否能加載到的問題, //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 {//當(dāng)還是加載不到,調(diào)用上層加載器
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());
}
}到此這篇關(guān)于深入理解ContextClassLoader加載器的文章就介紹到這了,更多相關(guān)ContextClassLoader加載器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在CentOS系統(tǒng)上安裝Java的openjdk的方法
這篇文章主要介紹了在CentOS系統(tǒng)上安裝Java的openjdk的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-06-06
一文詳解SpringBoot?Redis多數(shù)據(jù)源配置
Spring?Boot默認(rèn)只允許一種?Redis?連接池配置,且配置受限于?Lettuce?包,不夠靈活,所以本文將為大家介紹如何自定義Redis配置方案實(shí)現(xiàn)多數(shù)據(jù)源支持,需要的可以參考下2024-11-11
JAVA基于SnakeYAML實(shí)現(xiàn)解析與序列化YAML
這篇文章主要介紹了JAVA基于SnakeYAML實(shí)現(xiàn)解析與序列化YAML,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Java實(shí)戰(zhàn)之實(shí)現(xiàn)用戶登錄
這篇文章主要介紹了Java實(shí)戰(zhàn)之實(shí)現(xiàn)用戶登錄,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04

