av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Android-SPI學(xué)習(xí)筆記

瀏覽:3日期:2022-09-20 18:07:29
概述

SPI(Service Provider Interface, 服務(wù)提供方接口),服務(wù)通常是指一個(gè)接口或者一個(gè)抽象類,服務(wù)提供方是對(duì)這個(gè)接口或者抽象類的具體實(shí)現(xiàn),由第三方來實(shí)現(xiàn)接口提供具體的服務(wù)。通過解耦服務(wù)與其具體實(shí)現(xiàn)類,使得程序的可擴(kuò)展性大大增強(qiáng),甚至可插拔。基于服務(wù)的注冊(cè)與發(fā)現(xiàn)機(jī)制,服務(wù)提供者向系統(tǒng)注冊(cè)服務(wù),服務(wù)使用者通過查找發(fā)現(xiàn)服務(wù),可以達(dá)到服務(wù)的提供與使用的分離。

可以將 SPI 應(yīng)用到 Android 組件化中,很少直接使用 SPI,不過可基于它來擴(kuò)展其功能,簡(jiǎn)化使用步驟。

基本使用1. 在低層 module_common 中聲明服務(wù)

public interface IPrinter { void print();}2. 在上層 module 中實(shí)現(xiàn)服務(wù)

// 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// 可以配置多個(gè)實(shí)現(xiàn)類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 中使用服務(wù)

// 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) { // 獲取當(dāng)前線程的類加載器 ClassLoader cl = Thread.currentThread().getContextClassLoader(); return ServiceLoader.load(service, cl);}public static <S> ServiceLoader<S> load(Class<S> service, ClassLoader loader) { // 創(chuàng)建 ServiceLoader 實(shí)例 return new ServiceLoader<>(service, loader);}ServiceLoader實(shí)例創(chuàng)建

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(); // 創(chuàng)建了一個(gè)懶迭代器 lookupIterator = new LazyIterator(service, loader);}LazyIterator

ServiceLoader 實(shí)現(xiàn)了 Iterable 接口,可以使用 iterator/forEach 方法來迭代元素,其 iterator 方法實(shí)現(xiàn)如下:

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 緩存中已經(jīng)存在,則直接返回,否則加載 if (knownProviders.hasNext()) return knownProviders.next().getValue(); return lookupIterator.next(); } public void remove() { throw new UnsupportedOperationException(); } };}

上面使用了懶加載的方式,不至于一開始便去加載所有服務(wù)實(shí)現(xiàn),否則反射影響性能。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 {// 獲取服務(wù)配置文件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; } // 解析服務(wù)配置 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 { // 反射通過類加載器加載指定服務(wù) 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(); }}總結(jié)

ServiceLoader 的原理比較簡(jiǎn)單,其實(shí)就是使用一個(gè)懶迭代器,用時(shí)加載的方式可以減少性能損耗,在加載新服務(wù)的時(shí)候通過解析服務(wù)配置文件獲取配置的服務(wù),然后通過類加載器去加載配置的服務(wù)實(shí)現(xiàn)類,最后將其實(shí)例返回。

SPI的優(yōu)點(diǎn) 只提供服務(wù)接口,具體服務(wù)由其他組件實(shí)現(xiàn),接口和具體實(shí)現(xiàn)分離。 SPI的缺點(diǎn) 配置過于繁瑣 具體服務(wù)的實(shí)例化由ServiceLoader反射完成,生命周期不可控 當(dāng)存在多個(gè)實(shí)現(xiàn)類對(duì)象時(shí),ServiceLoader只提供了一個(gè)Iterator,無法精確拿到具體的實(shí)現(xiàn)類對(duì)象 需要讀取解析配置文件,性能損耗

以上就是Android-SPI學(xué)習(xí)筆記的詳細(xì)內(nèi)容,更多關(guān)于Android-SPI的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 国内精品久久久久久 | www在线视频 | 日韩一区和二区 | 亚洲免费精品 | 色婷婷综合久久久久中文一区二区 | 久久www免费人成看片高清 | 国产精品福利在线观看 | 亚洲乱码一区二区三区在线观看 | 国产成人精品亚洲日本在线观看 | 精品久久久久久久久久 | 日韩在线欧美 | 人和拘一级毛片c | 国产精品日日做人人爱 | 欧美在线一区二区三区 | 91网在线播放 | 国产99精品 | 日日夜夜天天综合 | 久久久久av | 毛片黄片| 欧美美女被c | 色综合99 | 国产一区二区三区在线 | 精品国产乱码一区二区三 | 欧美国产中文字幕 | 成人精品在线观看 | 伊人久久大香线 | 日韩精品一区二区三区在线观看 | 成人午夜影院 | 一级特黄网站 | 午夜欧美一区二区三区在线播放 | 成人免费淫片aa视频免费 | 国产专区视频 | 精品一区在线免费观看 | 久久免费高清 | 国产精品99久久免费观看 | 久久精品二区 | 久久久国产视频 | 一区二区三区在线播放 | 午夜理伦三级理论三级在线观看 | 91p在线观看 | 国产精品久久精品 |