ContextClassLoader深度講解.md

Thread.currentThread().getContextClassLoader();

  • 從方法名字來看,應(yīng)該是獲取當(dāng)前上下文的類加載器

那么問題來了,為什么要這樣設(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是單向的,沒辦法反過來從右邊找左邊.*

類加載器的委派雙親模式?

不明白的同學(xué),趕緊補(bǔ)習(xí)一波.

就是說當(dāng)我們this.getClass().getClassLoader();可以獲取到所有已經(jīng)加載過的文件,
但是System class loader -> Extension class loader -> Bootstrap class loader 就獲取不到ClassLoader A 能加載到的信息,那么怎么辦呢? 于是,Thread就把當(dāng)前的類加載器,給保存下來了,其他加載器,需要的時(shí)候,就把當(dā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)用上下文加載器

代碼塊分析

//獲取文件絕對地址,并不是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());
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容