首页 > 编程语言 >dubbo源码学习(二) : spring 自定义标签

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

时间:2023-06-04 13:02:30浏览次数:36  
标签:dubbo name 自定义 profession phone 源码 address public String


做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 



 <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

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

关注我可以获取it视频

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


标签:dubbo,name,自定义,profession,phone,源码,address,public,String
From: https://blog.51cto.com/u_13538361/6410431

相关文章

  • dbeaver 23启用从编程语言源码中提取SQL功能
    一直很喜欢dbeaver的一个自动提取剪切板SQL功能,该功能去除多余C#/Java中SQL字符串加号和双引号.但升级到dbeaver23之后,发现该功能默认被关闭了,开启功能见下图:......
  • 在 Spring 中自定义 scope
    大家对于Spring的scope应该都不会默认。所谓scope,字面理解就是“作用域”、“范围”,如果一个bean的scope配置为singleton,则从容器中获取bean返回的对象都是相同的;如果scope配置为prototype,则每次返回的对象都不同。一般情况下,Spring提供的scope都能满足日常应用的......
  • Flex/AS3/flash player支持屏蔽右键菜单,自定义菜单,并设置相应的菜单事件(示例,图解)..
    播放器版本11.2以后支持右键菜单屏蔽及自定义菜单1.更新播放器,11.2以上版本http://download.macromedia.com/get/flashplayer/updaters/11/playerglobal11_3.swchttp://download.macromedia.com/get/flashplayer/updaters/11/playerglobal11_4.swchttp://download.macro......
  • 【百行代码说游戏】ActionScript3.0 小游戏 【劲舞团】源码+演示
    最近学ActionScript3.0以下为自己写的一个小游戏。尽量以最少的代码,实现功能游戏原理:看代码注释游戏规则:类似于【劲舞团】游戏,玩家可以按UP,DOWN,LEFT,RIGHT键来操控游戏。打掉相应的箭头则得分,否则失手。箭头落到底线没有被打掉,则为失手。失手10次,游戏结束。得分过30,升一级。难度......
  • 组件的自定义事件
      专门用来看自定义事件的:  ......
  • Linux进程间通信源码分析
    概览这篇文章从内核源码的角度整理一下Linux的进程间通信机制。众所周知,Linux操作系统的通信机制有以下几种:信号管道(分为匿名管道和有名管道)信号量共享内存消息队列Socket本文主要内容包括其中前五个。其中信号量、共享内存、消息队列在Linux中有两套API,实现方式大不......
  • Request类源码分析、序列化组件介绍、序列化类的基本使用、常用字段类和参数、反序列
    目录一、Request类源码分析二、序列化组件介绍三、序列化类的基本使用查询所有和查询单条四、常用字段类和参数(了解)常用字段类字段参数(校验数据来用的)五、反序列化之校验六、反序列化之保存七、APIVIew+序列化类+Response写的五个接口代码八、序列化高级用法之source(了解)九、......
  • Dubbo入门
    目录简单整合SpringBoot示例dubbo与http接口区别一、http协议介绍二、dubbo介绍三、http协议和dubbo协议的区别特点Dubbo官网:https://cn.dubbo.apache.org/zh-cn/index.html简单整合SpringBoot示例gitee个人私有仓库地址:https://gitee.com/a_seagull/dubbo-testdubbo与http......
  • 使用vscode sftp插件快速上传源码文件
    1.首先安装vscode插件2.使用ctrl+shift+p或者view-commandpalette打开命令面板,输入sftp并按enter键,出现编辑配置文件界面3.输入对应的主机名,密码,或者密钥文件即可{"name":"47.100.101.152","host":"47.100.101.152","protocol":"sftp",......
  • LRU缓存与LinkedHashMap源码
    今天再刷LeetCode时,遇到了第146题LRU缓存。题目如下:请你设计并实现一个满足LRU(最近最少使用)缓存约束的数据结构。实现LRUCache类:LRUCache(intcapacity)以正整数作为容量capacity初始化LRU缓存intget(intkey)如果关键字key存在于缓存中,则返回关键字的值,否......