美文网首页
2021-12-19 深入理解Apache Dubbo与实战这本

2021-12-19 深入理解Apache Dubbo与实战这本

作者: 归去来ming | 来源:发表于2021-12-19 00:35 被阅读0次

这本书中的dubbo版本号,使用的是2.6.x的,项目中设置2.6.6即可

image.png

dubbo的SPI中的IOC和AOP机制,在哪里?

    // ExtensionLoader类,进入getExtension方法,有一个createExtension方法
    private T createExtension(String name) {
        Class<?> clazz = (Class)this.getExtensionClasses().get(name);
        if (clazz == null) {
            throw this.findException(name);
        } else {
            try {
                T instance = EXTENSION_INSTANCES.get(clazz);
                if (instance == null) {
                    EXTENSION_INSTANCES.putIfAbsent(clazz, clazz.newInstance());
                    instance = EXTENSION_INSTANCES.get(clazz);
                }
                // IOC
                this.injectExtension(instance);
                // AOP
                Set<Class<?>> wrapperClasses = this.cachedWrapperClasses;
                Class wrapperClass;
                if (wrapperClasses != null && !wrapperClasses.isEmpty()) {
                    for(Iterator var5 = wrapperClasses.iterator(); var5.hasNext(); instance = this.injectExtension(wrapperClass.getConstructor(this.type).newInstance(instance))) {
                        wrapperClass = (Class)var5.next();
                    }
                }

                return instance;
            } catch (Throwable var7) {
                throw new IllegalStateException("Extension instance(name: " + name + ", class: " + this.type + ")  could not be instantiated: " + var7.getMessage(), var7);
            }
        }
    }

// 继续分析injectExtension方法
private T injectExtension(T instance) {
    try {
        if (objectFactory != null) {
            for (Method method : instance.getClass().getMethods()) {
                if (method.getName().startsWith("set")
                        && method.getParameterTypes().length == 1
                        && Modifier.isPublic(method.getModifiers())) {
                    /**
                     * Check {@link DisableInject} to see if we need auto injection for this property
                     */
                    if (method.getAnnotation(DisableInject.class) != null) {
                        continue;
                    }
                    /**
                        setter方法只有一个参数,比如
                        public void setCar(Car car) {
                            this.car = car;
                        }
                        
                    */
                    Class<?> pt = method.getParameterTypes()[0];
                    try {
                        String property = method.getName().length() > 3 ? method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4) : "";
                        Object object = objectFactory.getExtension(pt, property);
                        if (object != null) {
                            method.invoke(instance, object);
                        }
                    } catch (Exception e) {
                        logger.error("fail to inject via method " + method.getName()
                                + " of interface " + type.getName() + ": " + e.getMessage(), e);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    return instance;
}

相关文章

  • 2021-12-19 深入理解Apache Dubbo与实战这本

    这本书中的dubbo版本号,使用的是2.6.x的,项目中设置2.6.6即可 dubbo的SPI中的IOC和AOP机...

  • Dubbo的线程模型、handler

    本系列参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6.1...

  • Dubbo的服务调用(消费端)

    本系列参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6.1...

  • Dubbo集群容错——Cluster

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

  • Dubbo集群容错——LoadBalance

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

  • Dubbo中的服务暴露

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

  • Dubbo集群容错——Directory

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

  • Dubbo中的服务引用

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

  • Dubbo集群容错——Mock

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

  • Dubbo集群容错——Router

    本系列主要参考官网文档、芋道源码的源码解读和《深入理解Apache Dubbo与实战》一书。Dubbo版本为2.6...

网友评论

      本文标题:2021-12-19 深入理解Apache Dubbo与实战这本

      本文链接:https://www.haomeiwen.com/subject/yupnfrtx.html