首先我们在SpringBoot的yaml文件中会有如下配置:
上面配置的是dubbo使用的协议,由于dubbo建议我们使用dubbo协议,所以配置如上。
Springboot加载的时候回读取yam文件,然后找到具体的实现类这时会用到ExtensionLoader<T> getExtensionLoader(Class<T> type) 方法。
加载第一步:
如下图:
加载第一步会传入上图中的type 接口类型,因为是第一次加载所以loader为空我们发现会创建ExtensionLoader对象。
public static <T> ExtensionLoader<T> getExtensionLoader(Class<T> type) {
if (type == null) {
throw new IllegalArgumentException("Extension type == null");
} else if (!type.isInterface()) {
throw new IllegalArgumentException("Extension type(" + type + ") is not interface!");
} else if (!withExtensionAnnotation(type)) {
throw new IllegalArgumentException("Extension type(" + type + ") is not extension, because WITHOUT @" + SPI.class.getSimpleName() + " Annotation!");
} else {
ExtensionLoader<T> loader = (ExtensionLoader)EXTENSION_LOADERS.get(type);
if (loader == null) {
EXTENSION_LOADERS.putIfAbsent(type, new ExtensionLoader(type));
loader = (ExtensionLoader)EXTENSION_LOADERS.get(type);
}
return loader;
}
}
看一下ExtensionLoader的构造方法:
private ExtensionLoader(Class<?> type) {
this.type = type;
this.objectFactory = type == ExtensionFactory.class ? null : (ExtensionFactory)getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension();
}
因为传入的不是ExtensionFactory 类所以会走三元运算符的后半部分,再次调用getExtensionLoader方法,传入ExtensionFactory类,紧接着再一次调用构造函数因为类型为ExtensionFactory 所以返回空,然后执行上一步返回,开始调用getAdaptiveExtension()方法,因为此时返回的loader对象中是ExtensionFactory对象,此时方法的下图部分执行完毕:
this.objectFactory = type == ExtensionFactory.class ? null : (ExtensionFactory)getExtensionLoader(ExtensionFactory.class)
然后开始执行getAdaptiveExtension()方法。
然后我们注意getAdaptiveExtension()方法中的下一行代码:
instance = this.createAdaptiveExtension();
跟进去:
private T createAdaptiveExtension() {
try {
return this.injectExtension(this.getAdaptiveExtensionClass().newInstance());
} catch (Exception var2) {
throw new IllegalStateException("Can not create adaptive extension " + this.type + ", cause: " + var2.getMessage(), var2);
}
}
注意下面,开始获取自适应拓展进行加载。
return this.injectExtension(this.getAdaptiveExtensionClass().newInstance());
private Class<?> getAdaptiveExtensionClass() {
this.getExtensionClasses();
return this.cachedAdaptiveClass != null ? this.cachedAdaptiveClass : (this.cachedAdaptiveClass = this.createAdaptiveExtensionClass());
}
我们注意:this.getExtensionClasses();这个是加载扩展点的实现类了。
classes = this.loadExtensionClasses()
里面在关注loadExtensionClasses( )方法。去指定目录下加载文件。
private Map<String, Class<?>> loadExtensionClasses() {
SPI defaultAnnotation = (SPI)this.type.getAnnotation(SPI.class);
if (defaultAnnotation != null) {
String value = defaultAnnotation.value();
if (value != null && (value = value.trim()).length() > 0) {
String[] names = NAME_SEPARATOR.split(value);
if (names.length > 1) {
throw new IllegalStateException("more than 1 default extension name on extension " + this.type.getName() + ": " + Arrays.toString(names));
}
if (names.length == 1) {
this.cachedDefaultName = names[0];
}
}
}
Map<String, Class<?>> extensionClasses = new HashMap();
this.loadFile(extensionClasses, "META-INF/dubbo/internal/");
this.loadFile(extensionClasses, "META-INF/dubbo/");
this.loadFile(extensionClasses, "META-INF/services/");
return extensionClasses;
}
然后创建自适应扩展类,
@Adaptive
public class AdaptiveExtensionFactory implements ExtensionFactory {
private final List<ExtensionFactory> factories;
public AdaptiveExtensionFactory() {
ExtensionLoader<ExtensionFactory> loader = ExtensionLoader.getExtensionLoader(ExtensionFactory.class);
List<ExtensionFactory> list = new ArrayList();
Iterator i$ = loader.getSupportedExtensions().iterator();
while(i$.hasNext()) {
String name = (String)i$.next();
list.add(loader.getExtension(name));
}
this.factories = Collections.unmodifiableList(list);
}
public <T> T getExtension(Class<T> type, String name) {
Iterator i$ = this.factories.iterator();
Object extension;
do {
if (!i$.hasNext()) {
return null;
}
ExtensionFactory factory = (ExtensionFactory)i$.next();
extension = factory.getExtension(type, name);
} while(extension == null);
return extension;
}
}
将加载到的SpringExtensionFactory 和 SpiExtensionFactory 注入到 factories中。
然后ExtensionLoader 中的private final ExtensionFactory objectFactory;有了值如下:
objectFactory中持有的是AdaptiveExtensionFactory的对象该对象的属性factories 是SpringExtensionFactory 和 SpiExtensionFactory 他们是ExtensionFactory 的两个实现类。AdaptiveExtensionFactory是使用的代理模式。
objectFactory是一个 ExtensionFactory,用来获取所有的托管bean