7.[dubbo源码解析]-xml解析

9 篇文章 0 订阅
1 篇文章 0 订阅

一.自定义spring标签解析

@From:《Spring文档-XML自定义标签的解析

  • 1.创建一个需要扩展的组件
  • 2.定义一个XSD文件描述文件描述组件内容
  • 3.创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义。
  • 4.创建一个Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器。
  • 5.编写Spring.Handler文件和Spring.schemas文件。

1.1.创建一个需要扩展的组件


Dubbodubbo-spring-configMETA-INF/spring.schemas 定义如下:

http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

1.2.定义一个XSD文件描述文件描述组件内容

dubbo.xsd

  • <xsd:element name="" />,定义了元素的名称。例如,<xsd:element name="application" />对应 <dubbo:application />
  • <xsd:element type="" />,定义了内建数据类型的名称。例如,<xsd:element type="applicationType" />对应 <xsd:complexType name="applicationType" />
  • <xsd:complexType name=""> ,定义了复杂类型。例如 <xsd:complexType name="applicationType" />

1.3.创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义。

1.4.创建一个Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器。

1.5.编写Spring.Handler文件和Spring.schemas文件。

Spring.Handler:

http\://dubbo.apache.org/schema/dubbo=org.apache.dubbo.config.spring.schema.DubboNamespaceHandler
http\://code.alibabatech.com/schema/dubbo=org.apache.dubbo.config.spring.schema.DubboNamespaceHandler

Spring.schemas:

http\://dubbo.apache.org/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd
http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/compat/dubbo.xsd

二.解析过程分析

解析器org.apache.dubbo.config.spring.schema.DubboBeanDefinitionParser
构造方法初始化解析器:
DubboNamespaceHandler.init()

    @Override
    public void init() {
        registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
        registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
        registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
        registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
        registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
        registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
        registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
        registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());
    }

DubboBeanDefinitionParser构造方法:

/**
 * Bean 对象的类
 */
private final Class<?> beanClass;
/**
 * 是否需要 Bean 的 `id` 属性
 */
private final boolean required;

public DubboBeanDefinitionParser(Class<?> beanClass, boolean required) {
    this.beanClass = beanClass;  
    this.required = required; @ sjt2
}

@sjt1: ,Bean 对象的类。

@sjt2: 是否需要在 Bean 对象的编号( id ) 不存在时,自动生成编号。无需被其他应用引用的配置对象,无需自动生成编号。例如有 <dubbo:reference />

2.1.DubboBeanDefinitionParser构造方法&parse()分析

2.1.1.创建:RootBeanDefinition
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
beanDefinition.setLazyInit(false);

lazyInit默认为false

@From: 《Dubbo 用户指南 —— XML 配置

引用缺省是延迟初始化的,只有引用被注入到其它 Bean,或被 getBean() 获取,才会初始化。如果需要饥饿加载,即没有人引用也立即生成动态代理,可以配置:<dubbo:reference … init=“true” />

2.1.2.处理 Bean 的编号
// 解析配置对象的 id 。若不存在,则进行生成。
        String id = element.getAttribute("id");
        if ((id == null || id.length() == 0) && required) {
            // 生成 id 。不同的配置对象,会存在不同。
            String generatedBeanName = element.getAttribute("name");
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                if (ProtocolConfig.class.equals(beanClass)) {
                    generatedBeanName = "dubbo";
                } else {
                    generatedBeanName = element.getAttribute("interface");
                }
            }
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                generatedBeanName = beanClass.getName();
            }
            id = generatedBeanName;
            // 若 id 已存在,通过自增序列,解决重复。
            int counter = 2;
            while (parserContext.getRegistry().containsBeanDefinition(id)) {
                id = generatedBeanName + (counter++);
            }
        }
        if (id != null && id.length() > 0) {
            if (parserContext.getRegistry().containsBeanDefinition(id)) {
                throw new IllegalStateException("Duplicate spring bean id " + id);
            }
            // 添加到 Spring 的注册表
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
            // 设置 Bean 的 `id` 属性值
            beanDefinition.getPropertyValues().addPropertyValue("id", id);
        }

分析: 解析Bean的id。若不存在,就按照name>特殊规则>className来自动生成。如id再Spring注册表已经存在,通过添加自增序列作为后缀,避免冲突来添加到String注册表中。

2.1.2.处理 dubbo:protocol/的情况
if (ProtocolConfig.class.equals(beanClass)) {
            // eg:
            // <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" protocol="dubbo" ref="demoService"/>
            // <dubbo:protocol id="dubbo" name="dubbo" port="20880"/>
            for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
                BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
                PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
                if (property != null) {
                    Object value = property.getValue();
                    if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
                        definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
                    }
                }
            }
        }

  • value instanceof ProtocolConfig谜之不清楚这一步是干啥的。TODO
2.1.3.处理 <dubbo:service /> 的 class 属性
else if (ServiceBean.class.equals(beanClass)) {
            //处理clas属性:<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" class="org.apache.dubbo.demo.DemoServiceImpl"/>
            String className = element.getAttribute("class");
            if (className != null && className.length() > 0) {
                // 创建 Service 的 RootBeanDefinition 对象。相当于内嵌了 <bean class="org.apache.dubbo.demo.DemoServiceImpl" />
                RootBeanDefinition classDefinition = new RootBeanDefinition();
                classDefinition.setBeanClass(ReflectUtils.forName(className));
                classDefinition.setLazyInit(false);
                // 解析 Service Bean 对象的属性
                parseProperties(element.getChildNodes(), classDefinition);
                // 设置 `<dubbo:service ref="" />` 属性
                beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
            }
        }

处理<dubbo:service class= "org.apache.dubbo.demo.DemoServiceImpl"/>这种情况

  • 处理 <dubbo:service class="" /> 场景下的处理,大多数情况下我们不这么使用,包括官方文档也没提供这种方式的说明。当配置 class 属时,会自动创建 Service Bean 对象,而无需再配置ref属性,指向 Service Bean 对象。示例如下:
<bean id="demoDAO" class="com.alibaba.dubbo.demo.provider.DemoDAO" />

<dubbo:service id="sa" interface="com.alibaba.dubbo.demo.DemoService"  class="com.alibaba.dubbo.demo.provider.DemoServiceImpl">
    <property name="demoDAO" ref="demoDAO" />
</dubbo:service>
2.1.4.解析 <dubbo:provider /> 的内嵌子元素 <dubbo:service />
  //解析 `<dubbo:provider />` 的内嵌子元素 `<dubbo:service />`
        } else if (ProviderConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
        } 
  • parseNested()
2.1.5. 解析 <dubbo:consumer /> 的内嵌子元素 <dubbo:reference />
 // 解析 `<dubbo:consumer />` 的内嵌子元素 `<dubbo:reference />`
        } else if (ConsumerConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
        }
  • parseNested()
2.1.6.循环 Bean 对象的 setting 方法,将属性赋值到 Bean 对象
Set<String> props = new HashSet<String>(); 已解析的属性集合
        // 循环 Bean 对象的 setting 方法,将属性添加到 Bean 对象的属性赋值
        ManagedMap parameters = null;
        for (Method setter : beanClass.getMethods()) {
            String name = setter.getName();
            if (name.length() > 3 && name.startsWith("set")
                    && Modifier.isPublic(setter.getModifiers())
                    && setter.getParameterTypes().length == 1) {
                Class<?> type = setter.getParameterTypes()[0];
                // 添加 `props`
                String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
                props.add(property);
                // getting && public && 属性值类型统一
                Method getter = null;
                try {
                    getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e) {
                    try {
                        getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
                    } catch (NoSuchMethodException e2) {
                    }
                }
                if (getter == null
                        || !Modifier.isPublic(getter.getModifiers())
                        || !type.equals(getter.getReturnType())) {
                    continue;
                }
                // 解析 `<dubbo:parameters />`
                if ("parameters".equals(property)) {
                    parameters = parseParameters(element.getChildNodes(), beanDefinition);
                    // 解析 `<dubbo:method />`
                } else if ("methods".equals(property)) {
                    parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
                    // 解析 `<dubbo:argument />`
                } else if ("arguments".equals(property)) {
                    parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
                } else {
                    String value = element.getAttribute(property);
                    if (value != null) {
                        value = value.trim();
                        if (value.length() > 0) {
                            // 不想注册到注册中心的情况,即 `registry=N/A`
                            if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
                                RegistryConfig registryConfig = new RegistryConfig();
                                registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
                                beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
                                // 多注册中心的情况
                            } else if ("registry".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("registries", value, beanDefinition, parserContext);
                                // 多服务提供者的情况
                            } else if ("provider".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("providers", value, beanDefinition, parserContext);
                                // 多协议的情况
                            } else if ("protocol".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("protocols", value, beanDefinition, parserContext);
                            } else {
                                Object reference;
                                // 处理属性类型为基本属性的情况
                                if (isPrimitive(type)) {
                                    // 兼容性处理
                                    if ("async".equals(property) && "false".equals(value)
                                            || "timeout".equals(property) && "0".equals(value)
                                            || "delay".equals(property) && "0".equals(value)
                                            || "version".equals(property) && "0.0.0".equals(value)
                                            || "stat".equals(property) && "-1".equals(value)
                                            || "reliable".equals(property) && "false".equals(value)) {
                                        // backward compatibility for the default value in old version's xsd
                                        value = null;
                                    }
                                    reference = value;
                                    // 处理在 `<dubbo:provider />` 或者 `<dubbo:service />` 上定义了 `protocol` 属性的 兼容性。
                                } else if ("protocol".equals(property)
                                        && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)// 存在该注册协议的实现
                                        && (!parserContext.getRegistry().containsBeanDefinition(value)// Spring 注册表中不存在该 `<dubbo:provider />` 的定义
                                        // Spring 注册表中存在该编号,但是类型不为 ProtocolConfig 。
                                        || !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                    // 目前,`<dubbo:provider protocol="" />` 推荐独立成 `<dubbo:protocol />`
                                    if ("dubbo:provider".equals(element.getTagName())) {
                                        logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
                                    }
                                    // backward compatibility
                                    ProtocolConfig protocol = new ProtocolConfig();
                                    protocol.setName(value);
                                    reference = protocol;
                                    // 处理 `onreturn` 属性
                                } else if ("onreturn".equals(property)) {
                                    // 按照 `.` 拆分
                                    int index = value.lastIndexOf(".");
                                    String returnRef = value.substring(0, index);
                                    String returnMethod = value.substring(index + 1);
                                    // 创建 RuntimeBeanReference ,指向回调的对象
                                    reference = new RuntimeBeanReference(returnRef);
                                    // 设置 `onreturnMethod` 到 BeanDefinition 的属性值
                                    beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
                                    // 处理 `onthrow` 属性
                                } else if ("onthrow".equals(property)) {
                                    // 按照 `.` 拆分
                                    int index = value.lastIndexOf(".");
                                    String throwRef = value.substring(0, index);
                                    String throwMethod = value.substring(index + 1);
                                    // 创建 RuntimeBeanReference ,指向回调的对象
                                    reference = new RuntimeBeanReference(throwRef);
                                    // 设置 `onthrowMethod` 到 BeanDefinition 的属性值
                                    beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
                                } else if ("oninvoke".equals(property)) {
                                    int index = value.lastIndexOf(".");
                                    String invokeRef = value.substring(0, index);
                                    String invokeRefMethod = value.substring(index + 1);
                                    reference = new RuntimeBeanReference(invokeRef);
                                    beanDefinition.getPropertyValues().addPropertyValue("oninvokeMethod", invokeRefMethod);
                                    // 通用解析
                                } else {
                                    // 指向的 Service 的 Bean 对象,必须是单例
                                    if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
                                        BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                                        if (!refBean.isSingleton()) {
                                            throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value + "\" scope=\"singleton\" ...>");
                                        }
                                    }
                                    // 创建 RuntimeBeanReference ,指向 Service 的 Bean 对象
                                    reference = new RuntimeBeanReference(value);
                                }
                                // 设置 BeanDefinition 的属性值
                                beanDefinition.getPropertyValues().addPropertyValue(property, reference);
                            }
                        }
                    }
                }
            }
        }
2.1.7.将 XML 元素未遍历到的属性,添加到 parameters 集合中
// 将 XML 元素,未在上面遍历到的属性,添加到 `parameters` 集合中。目前测试下来,不存在这样的情况。
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (!props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        if (parameters != null) {
            beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
        }
        return beanDefinition;
    }

三.用于解析的辅助方法

3.1. parseProperties()

/**
    * 解析 <dubbo:service class="" /> 情况下,内涵的 `<property />` 的赋值。
     * @param nodeList 子元素数组
     * @param beanDefinition Bean 定义对象
    */
    private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) {
        if (nodeList != null && nodeList.getLength() > 0) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    if ("property".equals(node.getNodeName())
                            || "property".equals(node.getLocalName())) {
                        String name = ((Element) node).getAttribute("name");
                        if (name != null && name.length() > 0) {
                            String value = ((Element) node).getAttribute("value");
                            String ref = ((Element) node).getAttribute("ref");
                            // value
                            if (value != null && value.length() > 0) {
                                beanDefinition.getPropertyValues().addPropertyValue(name, value);
                                // ref
                            } else if (ref != null && ref.length() > 0) {
                                beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref));
                            } else {
                                throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />");
                            }
                        }
                    }
                }
            }
        }
    }
3.2. parseNested()-解析内嵌的指向的子 XML 元素

 /**
     * 解析内嵌的指向的子 XML 元素
     *
     * @param element        父 XML 元素
     * @param parserContext  Spring 解析上下文
     * @param beanClass      内嵌解析子元素的 Bean 的类
     * @param required       是否需要 Bean 的 `id` 属性
     * @param tag            标签
     * @param property       父 Bean 对象在子元素中的属性名
     * @param ref            指向
     * @param beanDefinition 父 Bean 定义对象
     */
    private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass, boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {
        NodeList nodeList = element.getChildNodes();
        if (nodeList != null && nodeList.getLength() > 0) {
            boolean first = true;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    if (tag.equals(node.getNodeName())
                            || tag.equals(node.getLocalName())) { // 这三行,判断是否为指定要解析的子元素
                        if (first) {
                            first = false;
                            String isDefault = element.getAttribute("default");
                            if (isDefault == null || isDefault.length() == 0) {
                                beanDefinition.getPropertyValues().addPropertyValue("default", "false");
                            }
                        }
                        // 解析子元素,创建 BeanDefinition 对象
                        BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);
                        // 设置子 BeanDefinition ,指向父 BeanDefinition 。
                        if (subDefinition != null && ref != null && ref.length() > 0) {
                            subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));
                        }
                    }
                }
            }
        }
    }
  • 只解析指定标签。目前有内嵌的 <dubbo:service /> 和 <dubbo:reference /> 标签。解析指定标签,创建子 Bean 对象。设置创建的子 Bean 对象,指向父 Bean 对象
3.3.parseMultiRef()-解析多指向的情况,例如多注册中心,多协议等等

/**
       * 解析多指向的情况,例如多注册中心,多协议等等。
      *
     * @param property 属性
     * @param value 值
       * @param beanDefinition Bean 定义对象
      * @param parserContext Spring 解析上下文
       */
    @SuppressWarnings("unchecked")
    private static void parseMultiRef(String property, String value, RootBeanDefinition beanDefinition,
                                      ParserContext parserContext) {
        String[] values = value.split("\\s*[,]+\\s*");
        ManagedList list = null;
        for (int i = 0; i < values.length; i++) {
            String v = values[i];
            if (v != null && v.length() > 0) {
                if (list == null) {
                    list = new ManagedList();
                }
                list.add(new RuntimeBeanReference(v));
            }
        }
        beanDefinition.getPropertyValues().addPropertyValue(property, list);
    }
  • 以 . 拆分值字符串,创建 RuntimeBeanReference 数组。设置 Bean 对象的指定属性值。
3.4.parseMethods()-解析 <dubbo:method /> 标签

/**
     * 解析 `<dubbo:method />`
     *
     * @param id             Bean 的 `id` 属性。
     * @param nodeList       子元素节点数组
     * @param beanDefinition Bean 定义对象
     * @param parserContext  解析上下文
     */
    @SuppressWarnings("unchecked")
    private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
                                     ParserContext parserContext) {
        if (nodeList != null && nodeList.getLength() > 0) {
            ManagedList methods = null;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {  // 这三行,判断值解析 `<dubbo:method />`
                        String methodName = element.getAttribute("name");
                        if (methodName == null || methodName.length() == 0) {
                            throw new IllegalStateException("<dubbo:method> name attribute == null");
                        }
                        if (methods == null) {
                            methods = new ManagedList();
                        }
                        // 解析 `<dubbo:method />`,创建 BeanDefinition 对象
                        BeanDefinition methodBeanDefinition = parse(((Element) node),
                                parserContext, MethodConfig.class, false);
                        // 添加到 `methods` 中
                        String name = id + "." + methodName;
                        BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(
                                methodBeanDefinition, name);
                        methods.add(methodBeanDefinitionHolder);
                    }
                }
            }
            if (methods != null) {
                beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
            }
        }
    }
3.4.parseArguments()-解析 <dubbo:argument /> 标签。

/**
     * 解析 `<dubbo:argument />`
     *
     * @param id Bean 的 `id` 属性。
     * @param nodeList 子元素节点数组
     * @param beanDefinition Bean 定义对象
     * @param parserContext 解析上下文
     */
    @SuppressWarnings("unchecked")
    private static void parseArguments(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
                                       ParserContext parserContext) {
        if (nodeList != null && nodeList.getLength() > 0) {
            ManagedList arguments = null; // 解析的参数数组
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    if ("argument".equals(node.getNodeName())
                            || "argument".equals(node.getLocalName())) { // 这三行,判断值解析 `<dubbo:argument />`
                        String argumentIndex = element.getAttribute("index");
                        if (arguments == null) {
                            arguments = new ManagedList();
                        }
                        // 解析 `<dubbo:argument />`,创建 BeanDefinition 对象
                        BeanDefinition argumentBeanDefinition = parse(((Element) node), parserContext, ArgumentConfig.class, false);
                        // 添加到 `arguments` 中
                        String name = id + "." + argumentIndex;
                        BeanDefinitionHolder argumentBeanDefinitionHolder = new BeanDefinitionHolder(argumentBeanDefinition, name);
                        arguments.add(argumentBeanDefinitionHolder);
                    }
                }
            }
            if (arguments != null) {
                beanDefinition.getPropertyValues().addPropertyValue("arguments", arguments);
            }
        }
    }
3.4.parseParameters()-解析 <dubbo:parameter /> 标签。

/**
     * 解析 `<dubbo:parameter />`
     *
     * @param nodeList 子元素节点数组
     * @param beanDefinition Bean 定义对象
     * @return 参数集合
     */
    @SuppressWarnings("unchecked")
    private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) {
        if (nodeList != null && nodeList.getLength() > 0) {
            ManagedMap parameters = null;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    if ("parameter".equals(node.getNodeName())
                            || "parameter".equals(node.getLocalName())) { // 这三行,只解析子元素中的 `<dubbo:parameter />`
                        if (parameters == null) {
                            parameters = new ManagedMap();
                        }
                        // 添加到参数集合
                        String key = ((Element) node).getAttribute("key");
                        String value = ((Element) node).getAttribute("value");
                        boolean hide = "true".equals(((Element) node).getAttribute("hide")); // <dubbo:parameter hide=“” /> 
                        if (hide) {
                            key = Constants.HIDE_KEY_PREFIX + key;
                        }
                        parameters.put(key, new TypedStringValue(value, String.class));
                    }
                }
            }
            return parameters;
        }
        return null;
    }
  • 解析的参数集合 parameters ,添加 “key” “value” 到 parameters 。## 一.自定义spring标签解析

@From:《Spring文档-XML自定义标签的解析

  • 1.创建一个需要扩展的组件
  • 2.定义一个XSD文件描述文件描述组件内容
  • 3.创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义。
  • 4.创建一个Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器。
  • 5.编写Spring.Handler文件和Spring.schemas文件。

1.1.创建一个需要扩展的组件


Dubbodubbo-spring-configMETA-INF/spring.schemas 定义如下:

http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

1.2.定义一个XSD文件描述文件描述组件内容

dubbo.xsd

  • <xsd:element name="" />,定义了元素的名称。例如,<xsd:element name="application" />对应 <dubbo:application />
  • <xsd:element type="" />,定义了内建数据类型的名称。例如,<xsd:element type="applicationType" />对应 <xsd:complexType name="applicationType" />
  • <xsd:complexType name=""> ,定义了复杂类型。例如 <xsd:complexType name="applicationType" />

1.3.创建一个文件,实现BeanDefinitionParser接口,用来解析XSD文件中的定义和组件定义。

1.4.创建一个Handler文件,扩展自NamespaceHandlerSupport,目的是将组件注册到Spring容器。

1.5.编写Spring.Handler文件和Spring.schemas文件。

Spring.Handler:

http\://dubbo.apache.org/schema/dubbo=org.apache.dubbo.config.spring.schema.DubboNamespaceHandler
http\://code.alibabatech.com/schema/dubbo=org.apache.dubbo.config.spring.schema.DubboNamespaceHandler

Spring.schemas:

http\://dubbo.apache.org/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd
http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/compat/dubbo.xsd

二.解析过程分析

解析器org.apache.dubbo.config.spring.schema.DubboBeanDefinitionParser
构造方法初始化解析器:
DubboNamespaceHandler.init()

    @Override
    public void init() {
        registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
        registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
        registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
        registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
        registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
        registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
        registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
        registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());
    }

DubboBeanDefinitionParser构造方法:

/**
 * Bean 对象的类
 */
private final Class<?> beanClass;
/**
 * 是否需要 Bean 的 `id` 属性
 */
private final boolean required;

public DubboBeanDefinitionParser(Class<?> beanClass, boolean required) {
    this.beanClass = beanClass;  
    this.required = required; @ sjt2
}

@sjt1: ,Bean 对象的类。

@sjt2: 是否需要在 Bean 对象的编号( id ) 不存在时,自动生成编号。无需被其他应用引用的配置对象,无需自动生成编号。例如有 <dubbo:reference />

2.1.DubboBeanDefinitionParser构造方法&parse()分析

2.1.1.创建:RootBeanDefinition
RootBeanDefinition beanDefinition = new RootBeanDefinition();
beanDefinition.setBeanClass(beanClass);
beanDefinition.setLazyInit(false);

lazyInit默认为false

@From: 《Dubbo 用户指南 —— XML 配置

引用缺省是延迟初始化的,只有引用被注入到其它 Bean,或被 getBean() 获取,才会初始化。如果需要饥饿加载,即没有人引用也立即生成动态代理,可以配置:<dubbo:reference … init=“true” />

2.1.2.处理 Bean 的编号
// 解析配置对象的 id 。若不存在,则进行生成。
        String id = element.getAttribute("id");
        if ((id == null || id.length() == 0) && required) {
            // 生成 id 。不同的配置对象,会存在不同。
            String generatedBeanName = element.getAttribute("name");
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                if (ProtocolConfig.class.equals(beanClass)) {
                    generatedBeanName = "dubbo";
                } else {
                    generatedBeanName = element.getAttribute("interface");
                }
            }
            if (generatedBeanName == null || generatedBeanName.length() == 0) {
                generatedBeanName = beanClass.getName();
            }
            id = generatedBeanName;
            // 若 id 已存在,通过自增序列,解决重复。
            int counter = 2;
            while (parserContext.getRegistry().containsBeanDefinition(id)) {
                id = generatedBeanName + (counter++);
            }
        }
        if (id != null && id.length() > 0) {
            if (parserContext.getRegistry().containsBeanDefinition(id)) {
                throw new IllegalStateException("Duplicate spring bean id " + id);
            }
            // 添加到 Spring 的注册表
            parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
            // 设置 Bean 的 `id` 属性值
            beanDefinition.getPropertyValues().addPropertyValue("id", id);
        }

分析: 解析Bean的id。若不存在,就按照name>特殊规则>className来自动生成。如id再Spring注册表已经存在,通过添加自增序列作为后缀,避免冲突来添加到String注册表中。

2.1.2.处理 dubbo:protocol/的情况
if (ProtocolConfig.class.equals(beanClass)) {
            // eg:
            // <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" protocol="dubbo" ref="demoService"/>
            // <dubbo:protocol id="dubbo" name="dubbo" port="20880"/>
            for (String name : parserContext.getRegistry().getBeanDefinitionNames()) {
                BeanDefinition definition = parserContext.getRegistry().getBeanDefinition(name);
                PropertyValue property = definition.getPropertyValues().getPropertyValue("protocol");
                if (property != null) {
                    Object value = property.getValue();
                    if (value instanceof ProtocolConfig && id.equals(((ProtocolConfig) value).getName())) {
                        definition.getPropertyValues().addPropertyValue("protocol", new RuntimeBeanReference(id));
                    }
                }
            }
        }

  • value instanceof ProtocolConfig谜之不清楚这一步是干啥的。TODO
2.1.3.处理 <dubbo:service /> 的 class 属性
else if (ServiceBean.class.equals(beanClass)) {
            //处理clas属性:<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" class="org.apache.dubbo.demo.DemoServiceImpl"/>
            String className = element.getAttribute("class");
            if (className != null && className.length() > 0) {
                // 创建 Service 的 RootBeanDefinition 对象。相当于内嵌了 <bean class="org.apache.dubbo.demo.DemoServiceImpl" />
                RootBeanDefinition classDefinition = new RootBeanDefinition();
                classDefinition.setBeanClass(ReflectUtils.forName(className));
                classDefinition.setLazyInit(false);
                // 解析 Service Bean 对象的属性
                parseProperties(element.getChildNodes(), classDefinition);
                // 设置 `<dubbo:service ref="" />` 属性
                beanDefinition.getPropertyValues().addPropertyValue("ref", new BeanDefinitionHolder(classDefinition, id + "Impl"));
            }
        }

处理<dubbo:service class= "org.apache.dubbo.demo.DemoServiceImpl"/>这种情况

  • 处理 <dubbo:service class="" /> 场景下的处理,大多数情况下我们不这么使用,包括官方文档也没提供这种方式的说明。当配置 class 属时,会自动创建 Service Bean 对象,而无需再配置ref属性,指向 Service Bean 对象。示例如下:
<bean id="demoDAO" class="com.alibaba.dubbo.demo.provider.DemoDAO" />

<dubbo:service id="sa" interface="com.alibaba.dubbo.demo.DemoService"  class="com.alibaba.dubbo.demo.provider.DemoServiceImpl">
    <property name="demoDAO" ref="demoDAO" />
</dubbo:service>
2.1.4.解析 <dubbo:provider /> 的内嵌子元素 <dubbo:service />
  //解析 `<dubbo:provider />` 的内嵌子元素 `<dubbo:service />`
        } else if (ProviderConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ServiceBean.class, true, "service", "provider", id, beanDefinition);
        } 
  • parseNested()
2.1.5. 解析 <dubbo:consumer /> 的内嵌子元素 <dubbo:reference />
 // 解析 `<dubbo:consumer />` 的内嵌子元素 `<dubbo:reference />`
        } else if (ConsumerConfig.class.equals(beanClass)) {
            parseNested(element, parserContext, ReferenceBean.class, false, "reference", "consumer", id, beanDefinition);
        }
  • parseNested()
2.1.6.循环 Bean 对象的 setting 方法,将属性赋值到 Bean 对象
Set<String> props = new HashSet<String>(); 已解析的属性集合
        // 循环 Bean 对象的 setting 方法,将属性添加到 Bean 对象的属性赋值
        ManagedMap parameters = null;
        for (Method setter : beanClass.getMethods()) {
            String name = setter.getName();
            if (name.length() > 3 && name.startsWith("set")
                    && Modifier.isPublic(setter.getModifiers())
                    && setter.getParameterTypes().length == 1) {
                Class<?> type = setter.getParameterTypes()[0];
                // 添加 `props`
                String property = StringUtils.camelToSplitName(name.substring(3, 4).toLowerCase() + name.substring(4), "-");
                props.add(property);
                // getting && public && 属性值类型统一
                Method getter = null;
                try {
                    getter = beanClass.getMethod("get" + name.substring(3), new Class<?>[0]);
                } catch (NoSuchMethodException e) {
                    try {
                        getter = beanClass.getMethod("is" + name.substring(3), new Class<?>[0]);
                    } catch (NoSuchMethodException e2) {
                    }
                }
                if (getter == null
                        || !Modifier.isPublic(getter.getModifiers())
                        || !type.equals(getter.getReturnType())) {
                    continue;
                }
                // 解析 `<dubbo:parameters />`
                if ("parameters".equals(property)) {
                    parameters = parseParameters(element.getChildNodes(), beanDefinition);
                    // 解析 `<dubbo:method />`
                } else if ("methods".equals(property)) {
                    parseMethods(id, element.getChildNodes(), beanDefinition, parserContext);
                    // 解析 `<dubbo:argument />`
                } else if ("arguments".equals(property)) {
                    parseArguments(id, element.getChildNodes(), beanDefinition, parserContext);
                } else {
                    String value = element.getAttribute(property);
                    if (value != null) {
                        value = value.trim();
                        if (value.length() > 0) {
                            // 不想注册到注册中心的情况,即 `registry=N/A`
                            if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
                                RegistryConfig registryConfig = new RegistryConfig();
                                registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
                                beanDefinition.getPropertyValues().addPropertyValue(property, registryConfig);
                                // 多注册中心的情况
                            } else if ("registry".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("registries", value, beanDefinition, parserContext);
                                // 多服务提供者的情况
                            } else if ("provider".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("providers", value, beanDefinition, parserContext);
                                // 多协议的情况
                            } else if ("protocol".equals(property) && value.indexOf(',') != -1) {
                                parseMultiRef("protocols", value, beanDefinition, parserContext);
                            } else {
                                Object reference;
                                // 处理属性类型为基本属性的情况
                                if (isPrimitive(type)) {
                                    // 兼容性处理
                                    if ("async".equals(property) && "false".equals(value)
                                            || "timeout".equals(property) && "0".equals(value)
                                            || "delay".equals(property) && "0".equals(value)
                                            || "version".equals(property) && "0.0.0".equals(value)
                                            || "stat".equals(property) && "-1".equals(value)
                                            || "reliable".equals(property) && "false".equals(value)) {
                                        // backward compatibility for the default value in old version's xsd
                                        value = null;
                                    }
                                    reference = value;
                                    // 处理在 `<dubbo:provider />` 或者 `<dubbo:service />` 上定义了 `protocol` 属性的 兼容性。
                                } else if ("protocol".equals(property)
                                        && ExtensionLoader.getExtensionLoader(Protocol.class).hasExtension(value)// 存在该注册协议的实现
                                        && (!parserContext.getRegistry().containsBeanDefinition(value)// Spring 注册表中不存在该 `<dubbo:provider />` 的定义
                                        // Spring 注册表中存在该编号,但是类型不为 ProtocolConfig 。
                                        || !ProtocolConfig.class.getName().equals(parserContext.getRegistry().getBeanDefinition(value).getBeanClassName()))) {
                                    // 目前,`<dubbo:provider protocol="" />` 推荐独立成 `<dubbo:protocol />`
                                    if ("dubbo:provider".equals(element.getTagName())) {
                                        logger.warn("Recommended replace <dubbo:provider protocol=\"" + value + "\" ... /> to <dubbo:protocol name=\"" + value + "\" ... />");
                                    }
                                    // backward compatibility
                                    ProtocolConfig protocol = new ProtocolConfig();
                                    protocol.setName(value);
                                    reference = protocol;
                                    // 处理 `onreturn` 属性
                                } else if ("onreturn".equals(property)) {
                                    // 按照 `.` 拆分
                                    int index = value.lastIndexOf(".");
                                    String returnRef = value.substring(0, index);
                                    String returnMethod = value.substring(index + 1);
                                    // 创建 RuntimeBeanReference ,指向回调的对象
                                    reference = new RuntimeBeanReference(returnRef);
                                    // 设置 `onreturnMethod` 到 BeanDefinition 的属性值
                                    beanDefinition.getPropertyValues().addPropertyValue("onreturnMethod", returnMethod);
                                    // 处理 `onthrow` 属性
                                } else if ("onthrow".equals(property)) {
                                    // 按照 `.` 拆分
                                    int index = value.lastIndexOf(".");
                                    String throwRef = value.substring(0, index);
                                    String throwMethod = value.substring(index + 1);
                                    // 创建 RuntimeBeanReference ,指向回调的对象
                                    reference = new RuntimeBeanReference(throwRef);
                                    // 设置 `onthrowMethod` 到 BeanDefinition 的属性值
                                    beanDefinition.getPropertyValues().addPropertyValue("onthrowMethod", throwMethod);
                                } else if ("oninvoke".equals(property)) {
                                    int index = value.lastIndexOf(".");
                                    String invokeRef = value.substring(0, index);
                                    String invokeRefMethod = value.substring(index + 1);
                                    reference = new RuntimeBeanReference(invokeRef);
                                    beanDefinition.getPropertyValues().addPropertyValue("oninvokeMethod", invokeRefMethod);
                                    // 通用解析
                                } else {
                                    // 指向的 Service 的 Bean 对象,必须是单例
                                    if ("ref".equals(property) && parserContext.getRegistry().containsBeanDefinition(value)) {
                                        BeanDefinition refBean = parserContext.getRegistry().getBeanDefinition(value);
                                        if (!refBean.isSingleton()) {
                                            throw new IllegalStateException("The exported service ref " + value + " must be singleton! Please set the " + value + " bean scope to singleton, eg: <bean id=\"" + value + "\" scope=\"singleton\" ...>");
                                        }
                                    }
                                    // 创建 RuntimeBeanReference ,指向 Service 的 Bean 对象
                                    reference = new RuntimeBeanReference(value);
                                }
                                // 设置 BeanDefinition 的属性值
                                beanDefinition.getPropertyValues().addPropertyValue(property, reference);
                            }
                        }
                    }
                }
            }
        }
2.1.7.将 XML 元素未遍历到的属性,添加到 parameters 集合中
// 将 XML 元素,未在上面遍历到的属性,添加到 `parameters` 集合中。目前测试下来,不存在这样的情况。
        NamedNodeMap attributes = element.getAttributes();
        int len = attributes.getLength();
        for (int i = 0; i < len; i++) {
            Node node = attributes.item(i);
            String name = node.getLocalName();
            if (!props.contains(name)) {
                if (parameters == null) {
                    parameters = new ManagedMap();
                }
                String value = node.getNodeValue();
                parameters.put(name, new TypedStringValue(value, String.class));
            }
        }
        if (parameters != null) {
            beanDefinition.getPropertyValues().addPropertyValue("parameters", parameters);
        }
        return beanDefinition;
    }

三.用于解析的辅助方法

3.1. parseProperties()

/**
    * 解析 <dubbo:service class="" /> 情况下,内涵的 `<property />` 的赋值。
     * @param nodeList 子元素数组
     * @param beanDefinition Bean 定义对象
    */
    private static void parseProperties(NodeList nodeList, RootBeanDefinition beanDefinition) {
        if (nodeList != null && nodeList.getLength() > 0) {
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    if ("property".equals(node.getNodeName())
                            || "property".equals(node.getLocalName())) {
                        String name = ((Element) node).getAttribute("name");
                        if (name != null && name.length() > 0) {
                            String value = ((Element) node).getAttribute("value");
                            String ref = ((Element) node).getAttribute("ref");
                            // value
                            if (value != null && value.length() > 0) {
                                beanDefinition.getPropertyValues().addPropertyValue(name, value);
                                // ref
                            } else if (ref != null && ref.length() > 0) {
                                beanDefinition.getPropertyValues().addPropertyValue(name, new RuntimeBeanReference(ref));
                            } else {
                                throw new UnsupportedOperationException("Unsupported <property name=\"" + name + "\"> sub tag, Only supported <property name=\"" + name + "\" ref=\"...\" /> or <property name=\"" + name + "\" value=\"...\" />");
                            }
                        }
                    }
                }
            }
        }
    }
3.2. parseNested()-解析内嵌的指向的子 XML 元素

 /**
     * 解析内嵌的指向的子 XML 元素
     *
     * @param element        父 XML 元素
     * @param parserContext  Spring 解析上下文
     * @param beanClass      内嵌解析子元素的 Bean 的类
     * @param required       是否需要 Bean 的 `id` 属性
     * @param tag            标签
     * @param property       父 Bean 对象在子元素中的属性名
     * @param ref            指向
     * @param beanDefinition 父 Bean 定义对象
     */
    private static void parseNested(Element element, ParserContext parserContext, Class<?> beanClass, boolean required, String tag, String property, String ref, BeanDefinition beanDefinition) {
        NodeList nodeList = element.getChildNodes();
        if (nodeList != null && nodeList.getLength() > 0) {
            boolean first = true;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    if (tag.equals(node.getNodeName())
                            || tag.equals(node.getLocalName())) { // 这三行,判断是否为指定要解析的子元素
                        if (first) {
                            first = false;
                            String isDefault = element.getAttribute("default");
                            if (isDefault == null || isDefault.length() == 0) {
                                beanDefinition.getPropertyValues().addPropertyValue("default", "false");
                            }
                        }
                        // 解析子元素,创建 BeanDefinition 对象
                        BeanDefinition subDefinition = parse((Element) node, parserContext, beanClass, required);
                        // 设置子 BeanDefinition ,指向父 BeanDefinition 。
                        if (subDefinition != null && ref != null && ref.length() > 0) {
                            subDefinition.getPropertyValues().addPropertyValue(property, new RuntimeBeanReference(ref));
                        }
                    }
                }
            }
        }
    }
  • 只解析指定标签。目前有内嵌的 <dubbo:service /> 和 <dubbo:reference /> 标签。解析指定标签,创建子 Bean 对象。设置创建的子 Bean 对象,指向父 Bean 对象
3.3.parseMultiRef()-解析多指向的情况,例如多注册中心,多协议等等

/**
       * 解析多指向的情况,例如多注册中心,多协议等等。
      *
     * @param property 属性
     * @param value 值
       * @param beanDefinition Bean 定义对象
      * @param parserContext Spring 解析上下文
       */
    @SuppressWarnings("unchecked")
    private static void parseMultiRef(String property, String value, RootBeanDefinition beanDefinition,
                                      ParserContext parserContext) {
        String[] values = value.split("\\s*[,]+\\s*");
        ManagedList list = null;
        for (int i = 0; i < values.length; i++) {
            String v = values[i];
            if (v != null && v.length() > 0) {
                if (list == null) {
                    list = new ManagedList();
                }
                list.add(new RuntimeBeanReference(v));
            }
        }
        beanDefinition.getPropertyValues().addPropertyValue(property, list);
    }
  • 以 . 拆分值字符串,创建 RuntimeBeanReference 数组。设置 Bean 对象的指定属性值。
3.4.parseMethods()-解析 <dubbo:method /> 标签

/**
     * 解析 `<dubbo:method />`
     *
     * @param id             Bean 的 `id` 属性。
     * @param nodeList       子元素节点数组
     * @param beanDefinition Bean 定义对象
     * @param parserContext  解析上下文
     */
    @SuppressWarnings("unchecked")
    private static void parseMethods(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
                                     ParserContext parserContext) {
        if (nodeList != null && nodeList.getLength() > 0) {
            ManagedList methods = null;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    if ("method".equals(node.getNodeName()) || "method".equals(node.getLocalName())) {  // 这三行,判断值解析 `<dubbo:method />`
                        String methodName = element.getAttribute("name");
                        if (methodName == null || methodName.length() == 0) {
                            throw new IllegalStateException("<dubbo:method> name attribute == null");
                        }
                        if (methods == null) {
                            methods = new ManagedList();
                        }
                        // 解析 `<dubbo:method />`,创建 BeanDefinition 对象
                        BeanDefinition methodBeanDefinition = parse(((Element) node),
                                parserContext, MethodConfig.class, false);
                        // 添加到 `methods` 中
                        String name = id + "." + methodName;
                        BeanDefinitionHolder methodBeanDefinitionHolder = new BeanDefinitionHolder(
                                methodBeanDefinition, name);
                        methods.add(methodBeanDefinitionHolder);
                    }
                }
            }
            if (methods != null) {
                beanDefinition.getPropertyValues().addPropertyValue("methods", methods);
            }
        }
    }
3.4.parseArguments()-解析 <dubbo:argument /> 标签。

/**
     * 解析 `<dubbo:argument />`
     *
     * @param id Bean 的 `id` 属性。
     * @param nodeList 子元素节点数组
     * @param beanDefinition Bean 定义对象
     * @param parserContext 解析上下文
     */
    @SuppressWarnings("unchecked")
    private static void parseArguments(String id, NodeList nodeList, RootBeanDefinition beanDefinition,
                                       ParserContext parserContext) {
        if (nodeList != null && nodeList.getLength() > 0) {
            ManagedList arguments = null; // 解析的参数数组
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    if ("argument".equals(node.getNodeName())
                            || "argument".equals(node.getLocalName())) { // 这三行,判断值解析 `<dubbo:argument />`
                        String argumentIndex = element.getAttribute("index");
                        if (arguments == null) {
                            arguments = new ManagedList();
                        }
                        // 解析 `<dubbo:argument />`,创建 BeanDefinition 对象
                        BeanDefinition argumentBeanDefinition = parse(((Element) node), parserContext, ArgumentConfig.class, false);
                        // 添加到 `arguments` 中
                        String name = id + "." + argumentIndex;
                        BeanDefinitionHolder argumentBeanDefinitionHolder = new BeanDefinitionHolder(argumentBeanDefinition, name);
                        arguments.add(argumentBeanDefinitionHolder);
                    }
                }
            }
            if (arguments != null) {
                beanDefinition.getPropertyValues().addPropertyValue("arguments", arguments);
            }
        }
    }
3.4.parseParameters()-解析 <dubbo:parameter /> 标签。

/**
     * 解析 `<dubbo:parameter />`
     *
     * @param nodeList 子元素节点数组
     * @param beanDefinition Bean 定义对象
     * @return 参数集合
     */
    @SuppressWarnings("unchecked")
    private static ManagedMap parseParameters(NodeList nodeList, RootBeanDefinition beanDefinition) {
        if (nodeList != null && nodeList.getLength() > 0) {
            ManagedMap parameters = null;
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                if (node instanceof Element) {
                    if ("parameter".equals(node.getNodeName())
                            || "parameter".equals(node.getLocalName())) { // 这三行,只解析子元素中的 `<dubbo:parameter />`
                        if (parameters == null) {
                            parameters = new ManagedMap();
                        }
                        // 添加到参数集合
                        String key = ((Element) node).getAttribute("key");
                        String value = ((Element) node).getAttribute("value");
                        boolean hide = "true".equals(((Element) node).getAttribute("hide")); // <dubbo:parameter hide=“” /> 
                        if (hide) {
                            key = Constants.HIDE_KEY_PREFIX + key;
                        }
                        parameters.put(key, new TypedStringValue(value, String.class));
                    }
                }
            }
            return parameters;
        }
        return null;
    }
  • 解析的参数集合 parameters ,添加 “key” “value” 到 parameters 。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值