SPI原理-线程上下文类加载器

本文探讨了SPI(Service Provider Interface)原理,指出线程上下文类加载器(Thread.currentThread().getContextClassLoader())在SPI中的关键作用。SPI打破传统的类加载双亲委派模型,通过线程上下文类加载器来加载接口的具体实现,如jdbc驱动的加载,其核心方法ServiceLoader.load()的实现中就依赖了这一机制。
摘要由CSDN通过智能技术生成

   最近在看spring源码时,发现了一个方法ClassUtils.getDefaultClassLoader(),先看一下源码

   

public static ClassLoader getDefaultClassLoader(){
        ClassLoader cl = null;
        try {
            cl = Thread.currentThread().getContextClassLoader();
        }catch (Throwable ex){
            // Cannot access thread context ClassLoader - falling back...
        }
        if(cl == null){
            // No thread context class loader -> use class loader of this class.
            cl = ClassUtils.class.getClassLoader();
            if(cl == null){
                // getClassLoader() returning null indicates the bootstrap ClassLoader
                try {
                    cl = ClassLoader.getSystemClassLoader();
                }catch (Throwable ex){
                    // Cannot access system ClassLoader - oh well, maybe the caller can live with null...
                }
            }
        }
        return cl;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值