【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