看到这么多扩展类(每一个配置文件中都有很多) , 我们首先应该思考一个问题:Dubbo一启动 , 就加载所有的扩展类么?作为一个优秀的RPC框架 , 肯定不会耗时耗力做这样的无用功 , 所以肯定会通过一种方式拿到指定的扩展才对 。 我们可以看到大多是以键值对方式(表示为extName-value)配置的扩展 , 那么不难猜测 , 这里的extName就是用来实现上面所说的功能的 。 那到底是不是呢?以上纯属猜测 , 下面就到源码中去验证 。
SPI源码Dubbo中实现SPI的核心类是ExtensionLoader , 该类并未提供公共的构造方法来初始化 , 而是通过getExtensionLoader方法获取一个loader对象:
// loader缓存private static final ConcurrentMap, ExtensionLoader>> EXTENSION_LOADERS = new ConcurrentHashMap, ExtensionLoader>>();public staticExtensionLoader getExtensionLoader(Class type) {if (type == null)throw new IllegalArgumentException("Extension type == null");if(!type.isInterface()) {throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");}if(!withExtensionAnnotation(type)) {throw new IllegalArgumentException("Extension type(" + type +") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");}ExtensionLoader loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);if (loader == null) {EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));loader = (ExtensionLoader) EXTENSION_LOADERS.get(type);}return loader;}private final ExtensionFactory objectFactory;private ExtensionLoader(Class> type) {this.type = type;objectFactory = (type == ExtensionFactory.class ? null : ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension());}
这里的class参数就是扩展点的接口类型 , 每一个loader都需要绑定一个扩展点类型 。 然后首先从缓存中获取loader , 未获取到就初始化一个loader并放入缓存 。 而在私有构造器初始化的时候我们需要注意objectFactory这个变量 , 先大概有个映像 , 后面会用到 。 拿到loader之后 , 就可以调用getExtension方法去获取指定的扩展点了,该方法传入了一个name参数 , 不难猜测这个就是配置文件中的键 , 可以debugger验证一下:
private final ConcurrentMap> cachedInstances = new ConcurrentHashMap>();public T getExtension(String name) { if (name == null || name.length() == 0)throw new IllegalArgumentException("Extension name == null"); if ("true".equals(name)) {return getDefaultExtension(); } // 从缓存中获取Holder对象 , 该对象的值就是扩展对象 Holder
同样的也是先从缓存拿 , 缓存没有就创建并添加到缓存 , 因此主要看createExtension方法:
// 扩展类实例缓存对象private static final ConcurrentMap, Object> EXTENSION_INSTANCES = new ConcurrentHashMap
推荐阅读
-
在大学遭受欺凌,已接近毕业,怎样释怀有相关经历者吗
-
-
啸敏潮流科技不会对女人做这3件事(精辟),孟非:真正有本事的男人
-
「」这12个装修细节别漏了,过来人劝你,别等入住就后悔了!
-
支付宝|支付宝上线“划一划”密码:专为视障用户打造 更安全便捷
-
彩霞讲故事 韦小宝的原型,立功无数,后来被雍正收拾了,他是雍正的舅舅
-
天津新增1例境外输入确诊病例,为无症状感染者转确诊
-
-
-
东风-41弹道导弹|东风41导弹射中10000公里外目标,需要多久时间?
-
-
-
女仆报新闻|明星也要承担自己的错误,越混越差不怨别人
-
-
“尿不湿大侠”火了!跨奶瓶仗剑走天涯,网友笑翻:走一路尿一路
-
成成影视|凸显美女白皙的肌肤,长裙采用了无袖的设计
-
静若如初|非常仙的8款时尚套装,简约大方的款式,优雅又有女人味
-
[木一论史]只因女护士为10万美金,偷偷掐断氧气管,伊朗导弹专家窒息身亡
-
『搞笑小恶魔』都不好意思出轨了!,搞笑GIF趣图:没点演技
-