介绍:
Java的SPI(Service Provider Interface)机制是一种服务发现机制,它允许第三方为一个接口或抽象类提供实现,并使得应用程序可以在运行时发现和使用这些实现。SPI机制的核心思想是将接口实现类的全类名配置在一个文本文件中,应用程序通过读取这个文件来获取接口实现类的全类名,然后使用反射机制创建实例并调用方法。演示代码Github
SPI与API的区别:
从广义上来说它们都属于接口,而且很容易混淆。下面先用一张图说明一下:
SPI(Service Provider Interface)和API(Application Programming Interface)虽然都是接口,但它们在使用目的、使用阶段以及实现方式上存在区别。
- 使用目的:API是提供给外部开发者的一套预定义的方法或规则,用于规定软件组件之间如何交互。而SPI是为了使服务实现可插拔和可扩展,它允许在运行时动态地发现和加载服务实现。
- 使用阶段:API通常在开发阶段由调用方直接使用,定义了调用接口。SPI则主要被框架扩展人员使用,用于在应用程序中提供可插拔的实现,调用方可选择使用内置实现或自己实现。
- 实现方式:API的实现由提供方完成,并对外提供一组规则和约定供调用方使用。SPI的实现则是通过配置文件来指定具体实现类,这些实现类在程序运行时被动态加载。
总的来说,API主要用于定义组件间的交互标准,而SPI则是一种服务扩展机制,用于实现模块化开发中的动态加载和服务替换。
代码演示:
1、service-provider-interface
创建java 项目service-provider-interface ,创建接口Logger和类LoggerSerice,代码如下:
/** * @author 思维穿梭 */ public interface Logger { /** * info 级别打印日志 * @param message */ void info(String message); /** * debug 级别打印日志 * @param message */ void debug(String message); }
import java.util.ArrayList; import java.util.List; import java.util.ServiceLoader; /** * @author 思维穿梭 * */ public class LoggerService { private static final LoggerService SERVICE = new LoggerService(); private final Logger logger; private final List<Logger> loggerList; private LoggerService() { ServiceLoader<Logger> loader = ServiceLoader.load(Logger.class); List<Logger> list = new ArrayList<>(); for (Logger log :loader){ list.add(log); } loggerList = list; if (!list.isEmpty()){ logger = list.get(0); } else{ logger = null; } } public static LoggerService getService(){ return SERVICE; } public void info(String msg) { if (logger == null) { System.out.println("info 中没有发现 Logger 服务提供者"); } else { logger.info(msg); } } public void debug(String msg) { if (loggerList.isEmpty()) { System.out.println("debug 中没有发现 Logger 服务提供者"); } loggerList.forEach(log -> log.debug(msg)); } }
2、service-provider
创建java项目service-provider,将service-provider-interface项目jar包引入,创建Logger的实现类LogBack.
import com.dream.spi.log.Logger; /** * @author 思维穿梭 */ public class LogBack implements Logger { @Override public void info(String message) { System.out.println("LogBack info 打印日志: " + message); } @Override public void debug(String message) { System.out.println("LogBack debug 打印日志: " + message); } }
创建META-INF\services目录,在目录下常见Logger接口全名为为文件名的文件,文件中写入其实现类LogBack的全名称。如图:
3、spi-test
创建java项目spi-test。项目引入上面两个项目的jar包。创建测试类SpiTest,代码如下:
import com.dream.spi.log.LoggerService; /** * @author 思维穿梭 */ public class SpiTest { public static void main(String[] args) { // 这里可以调用SPI的实现类的方法 LoggerService loggerService = LoggerService.getService(); loggerService.info("你好"); loggerService.debug("测试Java SPI 机制"); } }
运行结果:
LogBack info 打印日志: 你好 LogBack debug 打印日志: 测试Java SPI 机制
ServiceLoader
ServiceLoader部分代码
public final class ServiceLoader<S> implements Iterable<S> { private static final String PREFIX = "META-INF/services/"; public static <S> ServiceLoader<S> load(Class<S> service) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); return ServiceLoader.load(service, cl); } public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) { return new ServiceLoader<>(service, loader); } private ServiceLoader(Class<S> svc, ClassLoader cl) { service = Objects.requireNonNull(svc, "Service interface cannot be null"); loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl; acc = (System.getSecurityManager() != null) ? AccessController.getContext() : null; reload(); } public void reload() { providers.clear(); lookupIterator = new LazyIterator(service, loader); } public Iterator<S> iterator() { return new Iterator<S>() { Iterator<Map.Entry<String,S>> knownProviders = providers.entrySet().iterator(); public boolean hasNext() { if (knownProviders.hasNext()) return true; return lookupIterator.hasNext(); } public S next() { if (knownProviders.hasNext()) return knownProviders.next().getValue(); return lookupIterator.next(); } public void remove() { throw new UnsupportedOperationException(); } }; } private class LazyIterator implements Iterator<S> { Class<S> service; ClassLoader loader; Enumeration<URL> configs = null; Iterator<String> pending = null; String nextName = null; private LazyIterator(Class<S> service, ClassLoader loader) { this.service = service; this.loader = loader; } private boolean hasNextService() { if (nextName != null) { return true; } if (configs == null) { try { String fullName = PREFIX + service.getName(); if (loader == null) configs = ClassLoader.getSystemResources(fullName); else configs = loader.getResources(fullName); } catch (IOException x) { fail(service, "Error locating configuration files", x); } } while ((pending == null) || !pending.hasNext()) { if (!configs.hasMoreElements()) { return false; } pending = parse(service, configs.nextElement()); } nextName = pending.next(); return true; } private S nextService() { if (!hasNextService()) throw new NoSuchElementException(); String cn = nextName; nextName = null; Class<?> c = null; try { c = Class.forName(cn, false, loader); } catch (ClassNotFoundException x) { fail(service, "Provider " + cn + " not found"); } if (!service.isAssignableFrom(c)) { fail(service, "Provider " + cn + " not a subtype"); } try { S p = service.cast(c.newInstance()); providers.put(cn, p); return p; } catch (Throwable x) { fail(service, "Provider " + cn + " could not be instantiated", x); } throw new Error(); // This cannot happen } }
LoggerService类中的代码作为入口。
public class LoggerService { private static final LoggerService SERVICE = new LoggerService(); private final Logger logger; private final List<Logger> loggerList; private LoggerService() { ServiceLoader<Logger> loader = ServiceLoader.load(Logger.class);
根据代码的调用顺序,在 reload()
方法中是通过一个内部类 LazyIterator
实现的。
private LoggerService() { ServiceLoader<Logger> loader = ServiceLoader.load(Logger.class); List<Logger> list = new ArrayList(); Iterator var3 = loader.iterator(); while(var3.hasNext()) { Logger log = (Logger)var3.next(); list.add(log); }
var3对应的是ServiceLoader的迭代器:
public Iterator<S> iterator() { return new Iterator<S>() { Iterator<Map.Entry<String,S>> knownProviders = providers.entrySet().iterator(); public boolean hasNext() { if (knownProviders.hasNext()) return true; return lookupIterator.hasNext(); } public S next() { if (knownProviders.hasNext()) return knownProviders.next().getValue(); return lookupIterator.next(); } public void remove() { throw new UnsupportedOperationException(); } }; }
lookupIterator = new LazyIterator(service, loader);
LazyIterator.hasNextService方法读取配置文件的内容。LazyIterator.
nextService中利用反射获取类。