Java的SPI(Service Provider Interface)机制是一种服务发现机制,它允许第三方为一个接口或抽象类提供实现,并使得应用程序可以在运行时发现和使用这些实现。SPI机制的核心思想是将接口实现类的全类名配置在一个文本文件中,应用程序通过读取这个文件来获取接口实现类的全类名,然后使用反射机制创建实例并调用方法。演示代码Github
从广义上来说它们都属于接口,而且很容易混淆。下面先用一张图说明一下:
SPI(Service Provider Interface)和API(Application Programming Interface)虽然都是接口,但它们在使用目的、使用阶段以及实现方式上存在区别。
总的来说,API主要用于定义组件间的交互标准,而SPI则是一种服务扩展机制,用于实现模块化开发中的动态加载和服务替换。
创建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 loggerList; private LoggerService() { ServiceLoader loader = ServiceLoader.load(Logger.class); List 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)); } }
创建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的全名称。如图:
创建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部分代码
public final class ServiceLoader implements Iterable { private static final String PREFIX = "META-INF/services/"; public static ServiceLoader load(Class service) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); return ServiceLoader.load(service, cl); } public static ServiceLoader load(Class service, ClassLoader loader) { return new ServiceLoader<>(service, loader); } private ServiceLoader(Class 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 iterator() { return new Iterator() { Iterator> 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 { Class service; ClassLoader loader; Enumeration configs = null; Iterator pending = null; String nextName = null; private LazyIterator(Class 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 loggerList; private LoggerService() { ServiceLoader loader = ServiceLoader.load(Logger.class);
根据代码的调用顺序,在 reload()
方法中是通过一个内部类 LazyIterator
实现的。
private LoggerService() { ServiceLoader loader = ServiceLoader.load(Logger.class); List list = new ArrayList(); Iterator var3 = loader.iterator(); while(var3.hasNext()) { Logger log = (Logger)var3.next(); list.add(log); }
var3对应的是ServiceLoader的迭代器:
public Iterator iterator() { return new Iterator() { Iterator> 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中利用反射获取类。