Dubbo|基础知识之解析自定义标签流程

Dubbo框架内自定义很多XML标签,方便以XML方式注册服务;本篇文章先了解下Dubbo框架拥有哪些XML标签,然后给出自定义标签的流程并动手自定义标签。

1.Dubbo框架中的那些标签

Dubbo框架标签定义的源文件是dubbo.xsd,该文件位于dubbo.jar内/META-INF/目录下;源文件内容太多就不一一列出来,不过从dubbo.xsd得知Dubbo拥有以下15个标签:annotation,application,module,registry,metadata-report,config-center,monitor,protocol,service,provider,consumer,reference,method,argument,parameters,每个标签都定义好各自的元素,元素几乎与各标签对应的实体类的属性相对应。

下面是使用dubbo自定义标签的一个例子。
自定义标签
为了后面更好的学习Dubbo框架定义标签的使用和解析,我们有必要了解标签自定义的过程。

2.自定义标签的流程
  • 定义标签实体类和.xsd文件
  • 声明标签的命名空间及其处理类
  • 声明标签的解析逻辑
  • 编写测试类
3.动手自定义标签
3.1 定义标签实体类和.xsd文件

标签实体类Person:

public class Person {
    private String name;
    private String age;
    private int sex;
    private boolean flag;
    
    // 省略set get方法,toString方法
}

定义person.xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
        xmlns="http://www.starry.net/schema/person"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:beans="http://www.springframework.org/schema/beans"
        targetNamespace="http://www.starry.net/schema/person"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans" />
    <!-- 定义element名, personType对应了bean的属性  -->
    <xsd:element name="person" type="personType">
        <xsd:annotation>
            <xsd:documentation><![CDATA[ The person config ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:element>
    <!--  配置各属性值,有点像Mybatis配置对应的model   -->
    <xsd:complexType name="personType">
        <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 person name. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="age" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The person age. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="sex" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The person sex. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="flag" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The person flag. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>

</xsd:schema>

person.xsd文件中的标签元数据对应Person.class类中的属性;.xsd文件中element的name值就是xml文件中使用的标签名,attribute的name值则是标签对应的属性元素。注意attribute的type值,不需要与实体类中属性的类型一致,统一写成string类型。

person.xsd文件放置在/resources目录下。

3.2 声明标签的命名空间及其处理类

person.xsd文件既然定义好了,那么如何使用呢?在xml文件头部通过自定义命名空间(xml name space)指定.xsd文件的schemasLocation,由该schemasLocation指定.xsd文件的位置。spring约定命名空间地址写在/META-INF/spring.handlers文件内,并且指定该命名空间的处理类,即此处的PersonNamespaceHandler.class。

http\://www.starry.net/schema/person=com.starry.bean.handler.PersonNameSpaceHandler

等号左边是自定义的命名空间地址,可以随便定义;等号右边是自定义命名空间处理类的类路径名
编写自定义命名空间处理类PersonNamespaceHandlers.class

package com.starry.bean.handler;

import com.starry.bean.parse.PersonBeanDefinitionParser;
import com.starry.custom.label.Person;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class PersonNameSpaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("person", new PersonBeanDefinitionParser(Person.class, true));
    }
}

命名空间指定.xsd文件的schemasLocation,一般都是配套写在xml文件的头部(命名空间地址和schemasLocation之间用空格或者换行隔开);spring约定schemaLocation写在/META-INF/ spring.schemas文件内,由schemationLocation地址指定person.xsd文件的位置。

http\://www.starry.net/schema/person/person.xsd=person.xsd

等号左边是约定的schemasLocation地址,它需要与上面定义的命名空间地址一致;等号右边则是person.xsd在项目中的位置,如果放置在/META-INF目录下,则需要指定目录为“/META-INF/person.xsd”。

3.3 声明标签的解析逻辑

在命名空间处理类PersonNamespaceHandler中有一个名为PersonBeanDefinitionParser类,该类是解析xml文件person标签的逻辑。

package com.starry.bean.parse;

import com.starry.custom.label.Person;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;


public class PersonBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {

    private final Class<?> beanClass;
    private final boolean required;

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

    protected Class getBeanClass(Element element) {
        return Person.class;
    }

    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String name = element.getAttribute("name");
        String age = element.getAttribute("age");
        String sex = element.getAttribute("sex");
        String flag = element.getAttribute("flag");
        if (StringUtils.hasText(name)) {
            builder.addPropertyValue("name", name);
        }

        if (StringUtils.hasText(age)) {
            builder.addPropertyValue("age", age);
        }

        if (StringUtils.hasText(sex)) {
            builder.addPropertyValue("sex", Integer.valueOf(sex));
        }

        if (StringUtils.hasText(flag)) {
            builder.addPropertyValue("flag", Boolean.valueOf(flag));
        }
    }
}

继承AbstractSingleBeanDefinitionParser类,重写doParse(Element,BeanDefinitionBuilder)方法,定义简单的解析逻辑。

3.4 编写测试类

新建person.xml文件,引入perosn命名空间以及schemasLocation;然后声明一个person标签的对象。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:test="http://www.starry.net/schema/person"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
         http://www.starry.net/schema/person
         http://www.starry.net/schema/person/person.xsd">

    <test:person id="a" name="xiaoMing" age="25" sex="1" flag="true" />

</beans>

xmlns:test="http://www.starry.net/schema/person"表示在person.xml文件内用test指定为person命名空间地址的简称,所以就有了test:perosn。person是person.xsd文件内定义的element名称,也是PersonNamespaceHandler.class处理person命名空间的依据。

注意:id属性是每个标签都有的,可以声明也可以省略。

编写test类,获取容器中person对象并打印出来。

package com.starry;

import com.starry.custom.label.Person;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("person.xml");
        Person person =  context.getBean(Person.class);
        System.out.println(person.toString());
    }
}

执行结果表明person.xml文件内test:person标签的内容已经被初始化为容器内的bean,所以自定义标签person成功。

4.思考

早在第二小节就给出了自定义标签的具体步骤,也是为了帮助不熟悉自定义标签的读者更好的理解这个过程;在第三节的演示过程中,详细说明每个步骤的作用和含义,虽然最终实践成功,但是你可能好奇PersonNamespaceHandler和PersonDefinitionParser类在自定义标签解析的过程中是如何执行的,或者说什么时候被执行的。

要想理解上面的问题,就必须深入ClassPathXmlApplicationContext类的初始化过程,这个过程我们在剖析Dubbo自定义标签解析流程中会深入分析,这里我们对自定义标签的流程有一个认识就OK了。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值