Dubbo#編譯動態擴展類

這篇排版有問題 後面修改....****java

以ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension();爲例spring

-->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, 擴展器)

下面是大體得過程圖this

相關文章
相關標籤/搜索