做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是如何解析、暴露服务。

关注我可以获取it视频

dubbo源码学习(二) : spring 自定义标签的更多相关文章

  1. Dubbo源码学习(二)

    @Adaptive注解 在上一篇ExtensionLoader的博客中记录了,有两种扩展点,一种是普通的扩展实现,另一种就是自适应的扩展点,即@Adaptive注解的实现类. @Documented ...

  2. Dubbo源码学习--服务是如何引用的

    ReferenceBean 跟服务引用一样,Dubbo的reference配置会被转成ReferenceBean类,ReferenceBean实现了InitializingBean接口,直接看afte ...

  3. Dubbo源码学习--服务是如何发布的

    相关文章: Dubbo源码学习--服务是如何发布的 Dubbo源码学习--服务是如何引用的 ServiceBean ServiceBean 实现ApplicationListener接口监听Conte ...

  4. Dubbo源码学习--优雅停机原理及在SpringBoot中遇到的问题

    Dubbo源码学习--优雅停机原理及在SpringBoot中遇到的问题 相关文章: Dubbo源码学习文章目录 前言 主要是前一阵子换了工作,第一个任务就是解决目前团队在 Dubbo 停机时产生的问题 ...

  5. Dubbo源码学习--注册中心分析

    相关文章: Dubbo源码学习--服务是如何发布的 Dubbo源码学习--服务是如何引用的 注册中心 关于注册中心,Dubbo提供了多个实现方式,有比较成熟的使用zookeeper 和 redis 的 ...

  6. Dubbo源码学习--集群负载均衡算法的实现

    相关文章: Dubbo源码学习文章目录 前言 Dubbo 的定位是分布式服务框架,为了避免单点压力过大,服务的提供者通常部署多台,如何从服务提供者集群中选取一个进行调用, 就依赖Dubbo的负载均衡策 ...

  7. Dubbo源码学习文章目录

    目录 Dubbo源码学习--服务是如何发布的 Dubbo源码学习--服务是如何引用的 Dubbo源码学习--注册中心分析 Dubbo源码学习--集群负载均衡算法的实现

  8. dubbo源码学习(二)dubbo容器启动流程简略分析

    dubbo版本2.6.3 继续之前的dubbo源码阅读,从com.alibaba.dubbo.container.Main.main(String[] args)作为入口 简单的数据一下启动的流程 1 ...

  9. 框架源码系列六:Spring源码学习之Spring IOC源码学习

    Spring 源码学习过程: 一.搞明白IOC能做什么,是怎么做的  1. 搞明白IOC能做什么? IOC是用为用户创建.管理实例对象的.用户需要实例对象时只需要向IOC容器获取就行了,不用自己去创建 ...

随机推荐

  1. 牛客小白月赛18 G Forsaken的三维数点

    思路: 这是一道树状数组和二分的题,用线段树空间直接爆,时间也会超 然后这道题我犯了一个很低级的错误,导致我wa了十发左右,一个int型变量用lld输入,然后他给的提示是运行错误,我哭了,我一直以为是 ...

  2. bitmat

    Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1138  Solved: 556[Submit][Status][Discuss] Descripti ...

  3. qs.parse()、qs.stringify()使用方法, 以及在axios 中怎么用?

    最近一直被纠结于传输格式,就在这里整理一下吧. qs是一个npm仓库所管理的包,可通过npm install qs命令进行安装.   (axios 自带qs , // import qs from ' ...

  4. SiteMesh 2.X 的使用(网页结构模板)

    SiteMesh是基于Servlet的filter的,即过滤流.它是通过截取reponse,并进行装饰后再交付给客户. 其中涉及到两个名词: 装饰页面(decorator page)和 "被 ...

  5. python之将Unicode文本标准化

    在需要比较字符串的程序中使用字符的多种表示会产生问题. 为了修正这个问题,你可以使用unicodedata模块先将文本标准化: s1 = 'Spicy Jalape\u00f1o' s2 = 'Spi ...

  6. android中的SQLite数据库

    SQLite是android中集成的一个轻量级的数据库,该数据库支持绝大部分SQL92语法 SQLiteDatabase代表一个数据库(底层就是一个数据库文件),一旦应用程序获得了代表指定数据库的SQ ...

  7. 【牛客网-剑指offer】用两个栈实现队列

    题目: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 知识点及概念: 队列:队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而 ...

  8. ICPC2008哈尔滨-E-Gauss Elimination

    题目描述 Li Zhixiang have already been in “Friendship” ocean-going freighter for three months. The excit ...

  9. 运行连接Oracle数据库时,Idea报错: Error : java 不支持发行版本5

    按照上面的截图步骤,一步步往下走,再运行程序时就不会报错了. 原文链接:https://blog.csdn.net/qq_22076345/article/details/82392236 感谢原文作 ...

  10. nodejs 静态资源文件与登陆交互

    server2.js var express=require('express'); var expressStatic=require('express-static'); var server=e ...