带你阅读dubbo源码之自定义标签(二)
来源:互联网 发布:51返利网 淘宝 编辑:程序博客网 时间:2024/05/14 20:28
头条号:不止于知识
使用过dubbo的人应该都配置过类似:
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />
那这些dubbo标签是怎么让spring容器识别的呢?
42.1 Introduction
Since version 2.0, Spring has featured a mechanism for schema-based extensions to the basic Spring XML format for defining and configuring beans. This section is devoted to detailing how you would go about writing your own custom XML bean definition parsers and integrating such parsers into the Spring IoC container.
To facilitate the authoring of configuration files using a schema-aware XML editor, Spring’s extensible XML configuration mechanism is based on XML Schema. If you are not familiar with Spring’s current XML configuration extensions that come with the standard Spring distribution, please first read the appendix entitled???.
Creating new XML configuration extensions can be done by following these (relatively) simple steps:
Authoring an XML schema to describe your custom element(s).
Coding a custom
NamespaceHandler
implementation (this is an easy step, don’t worry).Coding one or more
BeanDefinitionParser
implementations (this is where the real work is done).Registering the above artifacts with Spring (this too is an easy step).
spring官方文档中42.1点介绍中有详细说明怎么自定义标签,简单的来说自定义标签就是:
编写一个xml架构来描述自定义的元素
编写一个自定义的handler实现NamespaceHandler接口
编写一个或者多个自定义的解析器实现BeanDefinitionParser接口
注册到Spring容器
完成上述准备工作了,怎么让spring容器加载呢?
42.5 Registering the handler and the schema
The coding is finished! All that remains to be done is to somehow make the Spring XML parsing infrastructure aware of our custom element; we do this by registering our custom
namespaceHandler
and custom XSD file in two special purpose properties files. These properties files are both placed in a'META-INF'
directory in your application, and can, for example, be distributed alongside your binary classes in a JAR file. The Spring XML parsing infrastructure will automatically pick up your new extension by consuming these special properties files, the formats of which are detailed below.42.5.1 'META-INF/spring.handlers'
The properties file called
'spring.handlers'
contains a mapping of XML Schema URIs to namespace handler classes. So for our example, we need to write the following:http\://www.mycompany.com/schema/myns=org.springframework.samples.xml.MyNamespaceHandler
(The
':'
character is a valid delimiter in the Java properties format, and so the':'
character in the URI needs to be escaped with a backslash.)The first part (the key) of the key-value pair is the URI associated with your custom namespace extension, and needs to match exactly the value of the
'targetNamespace'
attribute as specified in your custom XSD schema.42.5.2 'META-INF/spring.schemas'
The properties file called
'spring.schemas'
contains a mapping of XML Schema locations (referred to along with the schema declaration in XML files that use the schema as part of the'xsi:schemaLocation'
attribute) to classpath resources. This file is needed to prevent Spring from absolutely having to use a defaultEntityResolver
that requires Internet access to retrieve the schema file. If you specify the mapping in this properties file, Spring will search for the schema on the classpath (in this case'myns.xsd'
in the'org.springframework.samples.xml'
package):http\://www.mycompany.com/schema/myns/myns.xsd=org/springframework/samples/xml/myns.xsd
The upshot of this is that you are encouraged to deploy your XSD file(s) right alongside the
NamespaceHandler
andBeanDefinitionParser
classes on the classpath.
spring官方文档在42.5*中有详细介绍,简单来说就是:
1.上述定义好的自定义元素描述文件文件(xsd)放置到META-INF目录下
例如:
<!-- myns.xsd (inside package org/springframework/samples/xml) --><?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns="http://www.mycompany.com/schema/myns"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.mycompany.com/schema/myns"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:element name="dateformat">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="lenient" type="xsd:boolean"/>
<xsd:attribute name="pattern" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element></xsd:schema>
2.定义好spring.handlers文件:
例如:http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
key:是你的命名空间,其他xml中通过引入这个命名空间进行使用该元素描述对象
value:指向的是你的handler
- 带你阅读dubbo源码之自定义标签(二)
- 带你阅读dubbo源码之通过SPI装载扩展(一)
- Dubbo源码阅读之 ExtensionLoader
- Taglib自定义标签之二带属性标签
- dubbo源码浅析(二)-标签解析
- Dubbo源码阅读之 客户端远程调用
- 自定义JSP中的Taglib标签之二带自定义属性的标签
- 自定义JSP中的Taglib标签之二带自定义属性的标签
- JSP的自定义标签(二)之带属性的标签
- testng源码阅读之二
- dubbo源码学习(三)基础知识:spring 自定义标签
- 读懂Dubbo源码必备知识点之二
- 关于dubbo自定义标签
- dubbo 自定义标签
- dubbo自定义标签
- Dubbo源码阅读之 服务端和客户端处理链
- 自定义标签之带属性的自定义标签
- Android 自定义View 带你飞(二)
- Spring Mail常见问题分析和解决办法
- Openwrt之gdb调试
- 【codevs1237】[网络流24题]餐巾计划问题
- Android上传开源项目(Library)到Jcenter
- Android version
- 带你阅读dubbo源码之自定义标签(二)
- python 中 常用到的 numpy 函数 整理
- 第三方支付相关知识结构
- task_struct各字段介绍
- Android 应用基础知识
- 【bzoj1010】[HNOI2008]玩具装箱toy
- 【bzoj1085】[SCOI2005]骑士精神
- 第一次机房收费系统—优化
- UVa 1632 Alibaba (区间dp)