dubbo源码学习(三)基础知识:spring 自定义标签
来源:互联网 发布:网络诈骗主题班会记录 编辑:程序博客网 时间:2024/05/29 07:20
初学dubbo的源码,只做尝试性的去学习,做为自己学习的一个记录,各位看官如果觉得写的有错误或理解的不对,请在留言区告诉我,互相学习。本人能力有限,有大神进入 时请指点。
我们在做dubbo的配置时很容易发现,dubbo有一套自己的标签,提供给开发者配置,其实每一个标签对应着一个 实体,在容器启动的时候,dubbo会对所有的配置进行解析然后将解析后的内容设置到实体里,最终dubbo会根据实体中的值生成贯穿全局的统一URL。利用自定义标签使配置简单明了化,与spring完美融合。
下面自己写一个自定义标签,主要需要如下 几个步骤:
1、编写实体类
2、编写Parser解析类
3、编写NameSpaceHandle类
4、配置spring.handlers
5、配置spring.schemas
6、配置chewl.xsd
实体类如下:
public class Chewl { 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 ChewlBeanDefinitionParserS extends AbstractSingleBeanDefinitionParser { private final Class<?> beanClass; private final boolean required; public ChewlBeanDefinitionParserS(Class<?> beanClass, boolean required) { this.beanClass = beanClass; this.required = required; } protected Class getBeanClass(Element element) { return Chewl.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 ChewlNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { //实现init方法,解析chewl标签 registerBeanDefinitionParser("chewl",new ChewlBeanDefinitionParserS(Chewl.class,true)); }}
spring.handlers配置,前面那一串其实可以随便配置,只要一会和后面的配置一致即可
http\://www.oschina.net/schema/chewl=springNameSpace.ChewlNamespaceHandlerspring.schemas配置
http\://www.oschina.net/schema/chewl/chewl.xsd=META-INF/chewl.xsdchewl.xsd的配置
<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns="http://www.oschina.net/schema/chewl" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.oschina.net/schema/chewl" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <!-- 定义element名,chewlType对应了bean的属性 --> <xsd:element name="chewl" type="chewlType"> <xsd:annotation> <xsd:documentation><![CDATA[ The chewl config ]]></xsd:documentation> </xsd:annotation> </xsd:element> <!-- 配置各属性值,有点像Mybatis配置对应的model --> <xsd:complexType name="chewlType"> <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 chewl name. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="age" type="xsd:int"> <xsd:annotation> <xsd:documentation><![CDATA[ The chewl age. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="profession" type="xsd:string"> <xsd:annotation> <xsd:documentation><![CDATA[ The chewl profession. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="address" type="xsd:string"> <xsd:annotation> <xsd:documentation><![CDATA[ The chewl address. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="phone" type="xsd:string"> <xsd:annotation> <xsd:documentation><![CDATA[ The chewl 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.oschina.net/schema/chewl" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.oschina.net/schema/chewl http://www.oschina.net/schema/chewl/chewl.xsd"> <common:chewl 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"); Chewl chewl = (Chewl) context.getBean("test"); System.out.println(chewl.toString()); }}
输出结果:
testchewenliang12技术bei jing18618152379
spring的自定义标签自己很容易实现,具体要看在实际项目中如何正确的实用它,接下来会记录dubbo是如何解析、暴露服务。
0 0
- dubbo源码学习(三)基础知识:spring 自定义标签
- Spring源码学习--自定义标签
- spring源码剖析(三)自定义标签实现及使用
- Dubbo源码分析(三):自定义Schema--基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
- dubbo源码学习(二)基础知识:JAVA SPI
- spring源码深度解析(笔记三)--自定义标签的解析
- dubbo源码 学习笔记(三)
- spring 自定义标签 学习
- spring源码学习之:xml配置文件标签自定义
- spring源码学习之:xml配置文件标签自定义
- Spring 源码解析 ---- 自定义标签
- struts自定义标签学习(三)
- dubbo 源码学习笔记 (三) —— dubbo引用服务的过程
- 带你阅读dubbo源码之自定义标签(二)
- Spring基础知识(三)
- dubbo 使用 学习三(spring+dubbo+zookeeper本地伪集群)
- 关于dubbo自定义标签
- dubbo 自定义标签
- Android AndFix热补丁动态修复框架使用教程
- sql service 存储过程,游标的使用
- Show me the code之Python练习册 Q13 获取网络图片
- C语言中char int转换问题
- 深入分析Java的序列化与反序列化
- dubbo源码学习(三)基础知识:spring 自定义标签
- OC学习日记Day3:数据在内存的储存形式,CPU查找内存数据
- 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件
- 礼拜四log~jQuery Validate验证框架
- log4j.xml详细配置
- Android 手动显示和隐藏软键盘
- pdf文件的页面怎么添加和删除
- AngularJs创建自己的Grid–分页组件
- navicat使用