码迷,mamicode.com
首页 > 其他好文 > 详细

05_dubbo_aop

时间:2018-09-08 21:05:49      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:mem   load   png   pie   分享图片   ssi   文件内容   rpc   ice   

【对这行代码进行源码分析】

ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class);
final Protocol dubboProtocol = loader.getExtension("dubbo");  //要分析的源码
final Protocol adaptiveExtension = loader.getAdaptiveExtension();

【对上面第二行代码进行源码分析】

先看下第一行代码执行后的ExtensionLoader实例化的结果

技术分享图片

其中

* type =interface com.alibaba.dubbo.rpc.Protocol

* ExtensionFactory objectFactory = AdaptiveExtensionFactory(适配类)

     * AdaptiveExtensionFactory成员变量factories = [ SpringExtensionFactory实例,SpiExtensionFactory实例 ]

 

【 getExtension(String name)源码 】

ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class);
final Protocol dubboProtocol = loader.getExtension("dubbo");
--createExtension(name);
----getExtensionClasses() //获取扩展类
------loadExtensionClasses()
--------loadFile(Map<String, Class<?>> extensionClasses, String dir)
//ioc属性注入
----injectExtension(instance); //aop+ioc属性注入 aop获得一个wrapper装饰类
----instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));

【  】

private T createExtension(String name) {
    /**
     * 从Holder<Map<String, Class<?>>>cachedClasses缓存中获取所有的实现类map,
     * 然后通过name获取到对应的实现类(此时只是一个对象实例,还没有属性注入)
     */
    Class<?> clazz = getExtensionClasses().get(name);
    if (clazz == null) {
        throw findException(name);
    }
    try {
        /**
         * 从 ConcurrentMap<Class<?>, Object> EXTENSION_INSTANCES 类缓存中获取到对应的实现类的Class对象,
         * 如果没有,直接创建,之后放入缓存
         */
        T instance = (T) EXTENSION_INSTANCES.get(clazz);
        if (instance == null) {
            EXTENSION_INSTANCES.putIfAbsent(clazz, (T) clazz.newInstance());
            instance = (T) EXTENSION_INSTANCES.get(clazz);
        }
        injectExtension(instance);
        Set<Class<?>> wrapperClasses = cachedWrapperClasses;
        if (wrapperClasses != null && wrapperClasses.size() > 0) {
            for (Class<?> wrapperClass : wrapperClasses) {
                instance = injectExtension((T) wrapperClass.getConstructor(type).newInstance(instance));
            }
        }
        return instance;
    } catch (Throwable t) {
        throw new IllegalStateException("Extension instance(name: " + name + ", class: " +
                type + ")  could not be instantiated: " + t.getMessage(), t);
    }
}

回头看下META-INF/dubbo/internal/com.alibaba.dubbo.rpc.Protocol文件内容

registry=com.alibaba.dubbo.registry.integration.RegistryProtocol
dubbo=com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol
filter=com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper
listener=com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper
mock=com.alibaba.dubbo.rpc.support.MockProtocol
injvm=com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol
rmi=com.alibaba.dubbo.rpc.protocol.rmi.RmiProtocol
hessian=com.alibaba.dubbo.rpc.protocol.hessian.HessianProtocol
com.alibaba.dubbo.rpc.protocol.http.HttpProtocol
com.alibaba.dubbo.rpc.protocol.webservice.WebServiceProtocol
thrift=com.alibaba.dubbo.rpc.protocol.thrift.ThriftProtocol
memcached=com.alibaba.dubbo.rpc.protocol.memcached.MemcachedProtocol

redis=com.alibaba.dubbo.rpc.protocol.redis.RedisProtocol

com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper和com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper,这两个类不含有@Adaptive注解且含有Protocol的单参数构造方法,符合这2个条件的类会被列入AOP增强类,会被防止在ExtensionLoader的实例缓存Set<Class<?>> cachedWrapperClasses;中。

此时的loader:

* type =interface com.alibaba.dubbo.rpc.Protocol

* ExtensionFactory objectFactory = AdaptiveExtensionFactory(适配类)

     * AdaptiveExtensionFactory成员变量factories = [ SpringExtensionFactory实例,SpiExtensionFactory实例 ]

* cachedWrapperClasses = [ class ProtocolListenerWrapper, class ProtocolFilterWrapper ]

技术分享图片

技术分享图片

再来看createExtension(String name )中的

 

05_dubbo_aop

标签:mem   load   png   pie   分享图片   ssi   文件内容   rpc   ice   

原文地址:https://www.cnblogs.com/HigginCui/p/9610192.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!