【JVM学习笔记】ServiceLoader类

ServiceLoader.load方法的函数原型如下

public static <S> ServiceLoader<S> load(Class<S> service)

其doc文档如下:

Creates a new service loader for the given service type, using the current thread's context class loader.
An invocation of this convenience method of the form
       ServiceLoader.load(service)
is equivalent to
       ServiceLoader.load(service,Thread.currentThread().getContextClassLoader())

Params:
  service – The interface or abstract class representing the service
Returns:
  A new service loader
Inferred annotations:
  @org.jetbrains.annotations.NotNull @org.jetbrains.annotations.Contract("_->new")

翻译后如下:

针对给定的类型,使用当前线程的上下文类加载器,创建一个新的service loader,
ServiceLoader.load(service) 这种简化的调用形式,实际上等同于 ServiceLoader.load(service,Thread.currentThread().getContextClassLoader())

Params:
  service - 表示服务的接口或抽象类

Returns:
  一个新的service loader

我的理解,该方法就是用来加载SPI (Service Provider Interface)

 

新建一个maven项目,引入mysql依赖包,然后运行以下代码

public class Test {
    public static void main(String[] args) {
        ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
        Iterator<Driver> iterator = loader.iterator();
        while (iterator.hasNext()) {
            Driver driver = iterator.next();
            System.out.println("driver: "+driver.getClass()+", loader: "+driver.getClass().getClassLoader());
        }

        System.out.println("当前线程上下文类加载器: "+Thread.currentThread().getContextClassLoader());
        System.out.println("ServiceLoader的累加载器: "+ServiceLoader.class.getClassLoader());
    }
}

运行结果

driver: class sun.jdbc.odbc.JdbcOdbcDriver, loader: null
driver: class com.mysql.jdbc.Driver, loader: sun.misc.Launcher$AppClassLoader@1c898b41
当前线程上下文类加载器: sun.misc.Launcher$AppClassLoader@1c898b41
ServiceLoader的累加载器: null

代码中的ServiceLoader和Driver都是JDK自带的类,为何ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);却能加载出MySql的驱动呢?

这其实是SPI的一种规范,在ServiceLoader类的doc文档里有具体说明,以加载mysql驱动为例,实际上规范要求第三方实现在jar包下面必须有META-INF/Service路径的文件夹,内含名为java.sql.Driver的文本文件,该文件名是接口的服务器名,表明该jar包含有的是这一类型服务的实现,文本文件的内容为com.mysql.jdbc.Driver,表明实现类的完全限定名。

转载于:https://www.cnblogs.com/heben/p/11456580.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值