Android-SPI學習筆記
SPI(Service Provider Interface, 服務提供方接口),服務通常是指一個接口或者一個抽象類,服務提供方是對這個接口或者抽象類的具體實現,由第三方來實現接口提供具體的服務。通過解耦服務與其具體實現類,使得程序的可擴展性大大增強,甚至可插拔。基于服務的注冊與發現機制,服務提供者向系統注冊服務,服務使用者通過查找發現服務,可以達到服務的提供與使用的分離。
可以將 SPI 應用到 Android 組件化中,很少直接使用 SPI,不過可基于它來擴展其功能,簡化使用步驟。
基本使用1. 在低層 module_common 中聲明服務public interface IPrinter { void print();}2. 在上層 module 中實現服務
// module_a -- implementation project(’:module_common’)// com.hearing.modulea.APrinterpublic class APrinter implements IPrinter { @Override public void print() { Log.d('LLL', 'APrinter'); }}// src/main/resources/META-INF/services/com.hearing.common.IPrinter// 可以配置多個實現類com.hearing.modulea.APrinter// ----------------------------------------------------------------//// module_b -- implementation project(’:module_common’)// com.hearing.moduleb.BPrinterpublic class BPrinter implements IPrinter { @Override public void print() { Log.d('LLL', 'BPrinter'); }}// src/main/resources/META-INF/services/com.hearing.common.IPrintercom.hearing.moduleb.BPrinter3. 在其它上層 module 中使用服務
// implementation project(’:module_common’)ServiceLoader<IPrinter> printers = ServiceLoader.load(IPrinter.class);for (IPrinter printer : printers) { printer.print();}ServiceLoader.load
ServiceLoader 的原理解析從 load 方法開始:
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) { // 創建 ServiceLoader 實例 return new ServiceLoader<>(service, loader);}ServiceLoader實例創建
private LinkedHashMap<String,S> providers = new LinkedHashMap<>();private ServiceLoader(Class<S> svc, ClassLoader cl) { service = Objects.requireNonNull(svc, 'Service interface cannot be null'); loader = (cl == null) ? ClassLoader.getSystemClassLoader() : cl; reload();}// Clear this loader’s provider cache so that all providers will be reloaded.public void reload() { providers.clear(); // 創建了一個懶迭代器 lookupIterator = new LazyIterator(service, loader);}LazyIterator
ServiceLoader 實現了 Iterable 接口,可以使用 iterator/forEach 方法來迭代元素,其 iterator 方法實現如下:
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() { // 如果 knownProviders 緩存中已經存在,則直接返回,否則加載 if (knownProviders.hasNext()) return knownProviders.next().getValue(); return lookupIterator.next(); } public void remove() { throw new UnsupportedOperationException(); } };}
上面使用了懶加載的方式,不至于一開始便去加載所有服務實現,否則反射影響性能。LazyIterator 類如下:
private static final String PREFIX = 'META-INF/services/';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) { // throw Exception } if (!service.isAssignableFrom(c)) { // throw Exception } try { S p = service.cast(c.newInstance()); providers.put(cn, p); return p; } catch (Throwable x) { // throw Exception } throw new Error(); // This cannot happen } public boolean hasNext() { return hasNextService(); } public S next() { return nextService(); } public void remove() { throw new UnsupportedOperationException(); }}總結
ServiceLoader 的原理比較簡單,其實就是使用一個懶迭代器,用時加載的方式可以減少性能損耗,在加載新服務的時候通過解析服務配置文件獲取配置的服務,然后通過類加載器去加載配置的服務實現類,最后將其實例返回。
SPI的優點 只提供服務接口,具體服務由其他組件實現,接口和具體實現分離。 SPI的缺點 配置過于繁瑣 具體服務的實例化由ServiceLoader反射完成,生命周期不可控 當存在多個實現類對象時,ServiceLoader只提供了一個Iterator,無法精確拿到具體的實現類對象 需要讀取解析配置文件,性能損耗以上就是Android-SPI學習筆記的詳細內容,更多關于Android-SPI的資料請關注好吧啦網其它相關文章!
相關文章: