6.2 dubbo在spring中自定义xml标签源码解析

时间:2023-01-07 14:41:16

6.1 如何在spring中自定义xml标签中我们看到了在spring中自定义xml标签的方式。dubbo也是这样来实现的。

6.2  dubbo在spring中自定义xml标签源码解析

一 META_INF/dubbo.xsd

比较长,只列出<dubbo:applicaton>元素相关的。

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns="http://code.alibabatech.com/schema/dubbo"
targetNamespace="http://code.alibabatech.com/schema/dubbo"> 。。。 <xsd:complexType name="applicationType">
<xsd:attribute name="id" type="xsd:ID">
<xsd:annotation>
<xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="name" type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application name. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="version" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application version. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="owner" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application owner name (email prefix). ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="organization" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The organization name. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="architecture" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The architecture. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="environment" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application environment, eg: dev/test/run ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="compiler" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The java code compiler. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="logger" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application logger. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="registry" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application registry. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="monitor" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application monitor. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="default" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ Is default. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType> 。。。 <xsd:element name="application" type="applicationType">
<xsd:annotation>
<xsd:documentation><![CDATA[ The application config ]]></xsd:documentation>
</xsd:annotation>
</xsd:element> 。。。 </xsd:schema>

与上一节完全相似。

二 META_INF/spring.schemas

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

与上一节完全相似。

三 DubboBeanDefinitionParser

代码较长,不再贴出来了,与上一节完全相似。

四 DubboNamespaceHandler

 package com.alibaba.dubbo.config.spring.schema;

 import com.alibaba.dubbo.common.Version;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ModuleConfig;
import com.alibaba.dubbo.config.MonitorConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.config.spring.ReferenceBean;
import com.alibaba.dubbo.config.spring.ServiceBean; import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class DubboNamespaceHandler extends NamespaceHandlerSupport { static {
Version.checkDuplicate(DubboNamespaceHandler.class);
} 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 DubboBeanDefinitionParser(AnnotationBean.class, true));
}
}

功能与上一节完全相似。这里可以看出,dubbo自定义了10个xml元素(也可以从xsd中看出)。从上边也可以看出,<dubbo:service>会被解析成ServiceBean,该bean极其重要。

五 META_INF/spring.handlers

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

与上一节完全相似。