dubbo源码学习(二) : spring 自定义标签

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 做dubbo的配置时很容易发现,dubbo有一套自己的标签,提供给开发者配置,其实每一个标签对应着一个 实体,在容器启动的时候,dubbo会对所有的配置进行解析然后将解析后的内容设置到实体里,最终dubbo会根据实体中的值生成贯穿全局的统一URL。利用自定义标签使配置简单明了化,与spring完美融合。

做dubbo的配置时很容易发现,dubbo有一套自己的标签,提供给开发者配置,其实每一个标签对应着一个 实体,在容器启动的时候,dubbo会对所有的配置进行解析然后将解析后的内容设置到实体里,最终dubbo会根据实体中的值生成贯穿全局的统一URL。利用自定义标签使配置简单明了化,与spring完美融合。


下面自己写一个自定义标签,主要需要如下 几个步骤:

 


1、编写实体类


2、编写Parser解析类


3、编写NameSpaceHandle类


4、配置spring.handlers


5、配置spring.schemas


6、配置customTag .xsd

 

标签实体类如下:


public class CustomTag {
    private String id;
    private String name;
    private Integer age;
    private String profession;
    private String address;
    private String phone;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getProfession() {
        return profession;
    }
    public void setProfession(String profession) {
        this.profession = profession;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append(id + "\n");
        sb.append(name + "\n");
        sb.append(age + "\n");
        sb.append(profession + "\n");
        sb.append(address + "\n");
        sb.append(phone + "\n");
        return sb.toString();
    }
}

 

标签的解析类如下:


public class CustomTagBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    private final Class<?> beanClass;
    private final boolean required;
    public CustomTagBeanDefinitionParser (Class<?> beanClass, boolean required) {
        this.beanClass = beanClass;
        this.required = required;
    }
    protected Class getBeanClass(Element element) {
        return CustomTag.class;
    }
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        //通过配置文件获取相应的值,设置到bean的属性中
        String id = element.getAttribute("id");
        String name = element.getAttribute("name");
        String age = element.getAttribute("age");
        String profession = element.getAttribute("profession");
        String address = element.getAttribute("address");
        String phone = element.getAttribute("phone");
        if (StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }
        if (StringUtils.hasText(name)) {
            builder.addPropertyValue("name", name);
        }
        if (StringUtils.hasText(age)) {
            builder.addPropertyValue("age", age);
        }
        if (StringUtils.hasText(profession)) {
            builder.addPropertyValue("profession", profession);
        }
        if (StringUtils.hasText(address)) {
            builder.addPropertyValue("address", address);
        }
        if (StringUtils.hasText(phone)) {
            builder.addPropertyValue("phone", phone);
        }
    }
}

 

NameSpaceHandle类如下:


public class CustomTagNamespaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        //实现init方法,解析CustomTag标签
        registerBeanDefinitionParser("customTag",new CustomTagBeanDefinitionParser(CustomTag.class,true));
    }
}
spring.handlers配置,前面那一串其实可以随便配置,只要一会和后面的配置一致即可
http\://www.51gitee.net/schema/customTag=springNameSpace.CustomTagNamespaceHandler
spring.schemas配置
http\://www.51gitee.net/schema/customTag/customTag.xsd=META-INF/customTag.xsd
customTag.xsd的配置
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
        xmlns="http://www.51gitee.net/schema/customTag"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:beans="http://www.springframework.org/schema/beans"
        targetNamespace="http://www.51gitee.net/schema/customTag"
        elementFormDefault="qualified"
        attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans" />
    <!-- 定义element名, customTagType对应了bean的属性  -->
    <xsd:element name="customTag" type="customTagType">
        <xsd:annotation>
            <xsd:documentation><![CDATA[ The customTag config ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:element>
    <!--  配置各属性值,有点像Mybatis配置对应的model   -->
    <xsd:complexType name="customTagType">
        <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 customTag name. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="age" type="xsd:int">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The customTag age. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="profession" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The customTag profession. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="address" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The customTag address. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="phone" type="xsd:string">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The customTag phone. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>
</xsd:schema>

 

最后测试


在新建一个spring的配置文件如下

<?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:common="http://www.51gitee.net/schema/customTag"
       xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.oschina.net/schema/customTag
     http://www.oschina.net/schema/customTag/customTag.xsd">
    <common:customTag id="test"  name="chewenliang" address="bei jing" age="12" phone="18618152379" profession="技术" />
</beans>

 

在java代码中测试


public class TestNameSpace {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-test.xml");
        CustomTag customTag= (CustomTag) context.getBean("test");
        System.out.println(customTag.toString());
    }
}

 

输出结果:


test  
    chewenliang  
    12  
    技术  
    bei jing  
    18618152379

 

spring的自定义标签自己很容易实现,具体要看在实际项目中如何正确的实用它,接下来会记录dubbo是如何解析、暴露服务。

相关文章
|
1月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的公考学习平台附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的公考学习平台附带文章源码部署视频讲解等
34 5
|
1月前
|
Java 数据格式 微服务
2024最新首发,全网最全 Spring Boot 学习宝典(附思维导图)
📚 《滚雪球学Spring Boot》是由CSDN博主bug菌创作的全面Spring Boot教程。作者是全栈开发专家,在多个技术社区如CSDN、掘金、InfoQ、51CTO等担任博客专家,并拥有超过20万的全网粉丝。该教程分为入门篇和进阶篇,每篇包含详细的教学步骤,涵盖Spring Boot的基础和高级主题。
111 4
2024最新首发,全网最全 Spring Boot 学习宝典(附思维导图)
|
1月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的在线学习过程管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线学习过程管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的在线学习过程管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
23天前
|
Java Spring 容器
Spring Boot 启动源码解析结合Spring Bean生命周期分析
Spring Boot 启动源码解析结合Spring Bean生命周期分析
60 11
|
20天前
|
安全 Java 数据库
三更草堂 Spring Security学习总结(思路整理)
Spring Security学习总结(思路整理)
|
23天前
|
缓存 Java 程序员
spring IoC 源码
spring IoC 源码
39 3
|
5天前
|
Dubbo Java Nacos
【实战攻略】破解Dubbo+Nacos+Spring Boot 3 Native打包后运行异常的终极秘籍——从零开始彻底攻克那些让你头疼不已的技术难题!
【8月更文挑战第15天】Nacos作为微服务注册与配置中心受到欢迎,但使用Dubbo+Nacos+Spring Boot 3进行GraalVM native打包后常遇运行异常。本文剖析此问题及其解决策略:确认GraalVM版本兼容性;配置反射列表以支持必要类和方法;采用静态代理替代动态代理;检查并调整配置文件;禁用不支持的功能;利用日志和GraalVM诊断工具定位问题;根据诊断结果调整GraalVM配置。通过系统排查方法,能有效解决此类问题,确保服务稳定运行。
19 0
|
1月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的大学生国学自主学习平台的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的大学生国学自主学习平台的详细设计和实现(源码+lw+部署文档+讲解等)
|
1月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的诗词学习系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的诗词学习系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
1月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的学生网课学习效果评价附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的学生网课学习效果评价附带文章源码部署视频讲解等
51 2