Dubbo#编译动态扩展类

这篇排版有问题 后面修改....****

以ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();为例

-->ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension()
  -->new ExtensionLoader<T>(Protocol.class)
     -->this.type = Protocol.class
     -->objectFactory = ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()
    -->new ExtensionLoader<T>(ExtensionFactory.class)
        -->this.type = ExtensionFactory.class
  	    -->objectFactory  = null
        -->getAdaptiveExtension()
       -->instance = createAdaptiveExtension
           -->AdaptiveExtensionFactory.class = getAdaptiveExtensionClass
    	  -->getExtensionClasses
    	     -->Map<String, Class<?>> classes = loadExtensionClasses		//加载扩展类
    	        -->loadDirectory 	//以此加载当个默认目录下的
    			com.alibaba.dubbo.common.extension.ExtensionFactory文件中的扩展类
    		   -->loadResource
    		      -->loadClass
    			文件中AdaptiveExtensionFactory类有@Adaptive注解
    			cachedAdaptiveClass = AdaptiveExtensionFactory.class
    			然后加载SpiExtensionFactory,SpringExtensionFactory, 并放入extensionClasses(Map)
    			cachedNames.put(SpiExtensionFactory.class, spi) 
    			cachedNames.put(SpringExtensionFactory.class, spring)
 		  	-->cachedClasses.set(classes )  
				此时cachedClasses中有了SpiExtensionFactory.class和SpringExtensionFactory.class
	       -->由于AdaptiveExtensionFactory.class给cachedAdaptiveClass赋值了,这个返回cachedAdaptiveClass
 	       -->injectExtension(AdaptiveExtensionFactory.class.newInstance)  
		**//IOC  通过set方法给属性赋值, 这里调用了构造方法,将cachedClasses中的值添加到factories中**

		**//这里就是AdaptiveExtensionFactory中factories值得由来**
	       -->cachedAdaptiveInstance.set(instance)  //将AdaptiveExtensionFactory对象保存起来
	 -->EXTENSION_LOADERS.put(ExtensionFactory.class, 扩展器) 
		 **//注意,这里将ExtensionFactory的扩展器保存在EXTENSION_LOADERS中了,这是个static变量**
	//以上都是ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getAdaptiveExtension()的步骤
	 -->getAdaptiveExtension
	-->createAdaptiveExtension
	   -->Protocol$Adaptive = getAdaptiveExtensionClass
	      -->getExtensionClasses
	     这里步骤与加载ExtensionFactory一样,加载com.alibaba.dubbo.rpc.Protocol下的扩展类
		由于该文件下没有@Adaptive修饰的类,执行createAdaptiveExtensionClass方法生成动态类
	       -->createAdaptiveExtensionClass
 		  -->createAdaptiveExtensionClassCode //创建动态类代码
		  -->AdaptiveComplier = ExtensionLoader.getExtensionLoader(Compiler.class)
							.getAdaptiveExtension();
		     -->new ExtensionLoader<T>(Compiler.class)
			-->this.type = Compiler.class;
			-->objectFactory = ExtensionLoader.getExtensionLoader(ExtensionFactory.class)
							.getAdaptiveExtension());
			   -->直接从EXTENSION_LOADERS中获取ExtensionFactory的扩展器
			   -->getAdaptiveExtension
			      -->createAdaptiveExtension
				 -->getExtensionClasses
					//Compiler文件中AdaptiveCompiler类被@Adaptive修饰,直接返回
				    -->AdaptiveCompiler.compile
				       -->AbstractCompiler.compile
					**//先使用Class.forName去加载该class文件**
					**//如果没有调用JavassistCompiler.doCompile  使用javassist去编译(cls.toClass)的到**
				**// 到这里从获取到扩展类到编译得到Class文件就完成了**
		     -->EXTENSION_LOADERS.put(Compiler.class, 扩展器)
	-->cachedAdaptiveInstance.set(Protocol$Adaptive);
  -->EXTENSION_LOADERS.put(Protocol.class, 扩展器)

下面是大致得过程图

posted @ 2019-06-12 00:02  复合式→展开式  阅读(258)  评论(0编辑  收藏  举报