深入理解ContextClassLoader加載器
ContextClassLoader
Thread.currentThread().getContextClassLoader(); - 從方法名字來看,應該是獲取當前上下文的類加載器 搞清楚這個問題, 當你在出現(xiàn)資源加載不到的時候就很容器解決
那么問題來了,為什么要這樣設計? 解決了什么樣的設計問題? 解決了什么樣的開發(fā)問題?
- 解決委派雙親加載模式的缺點
- 實現(xiàn)了JNDI等
- 解決開發(fā)中,文件加載不到的異常
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();可以獲取到所有已經(jīng)加載過的文件, 但是 System class loader -> Extension class loader -> Bootstrap class loader 就獲取不到 ClassLoader A 能加載到的信息,那么怎么辦呢? 于是,Thread就把當前的類加載器,給保存下來了,其他加載器,需要的時候,就把當前線程的加載器,獲取到.
那么什么場景下,會遇到這種情況那,當通常發(fā)生在有些JVM核心代碼必須動態(tài)加載由應用程序開發(fā)人員提供的資源時eg:
JNDI
JNDI(Java Naming and Directory Interface,Java命名和目錄接口)是SUN公司提供的一種標準的Java命名系統(tǒng)接口,JNDI提供統(tǒng)一的客戶端API,通過不同的訪問提供者接口
在系統(tǒng)中,要調用開發(fā)者的資源,此時就遇到了這種情況
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(); //當發(fā)布到線上只發(fā)布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加載器內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
在CentOS系統(tǒng)上安裝Java的openjdk的方法
這篇文章主要介紹了在CentOS系統(tǒng)上安裝Java的openjdk的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-06-06一文詳解SpringBoot?Redis多數(shù)據(jù)源配置
Spring?Boot默認只允許一種?Redis?連接池配置,且配置受限于?Lettuce?包,不夠靈活,所以本文將為大家介紹如何自定義Redis配置方案實現(xiàn)多數(shù)據(jù)源支持,需要的可以參考下2024-11-11JAVA基于SnakeYAML實現(xiàn)解析與序列化YAML
這篇文章主要介紹了JAVA基于SnakeYAML實現(xiàn)解析與序列化YAML,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12