带你阅读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 customNamespaceHandlerimplementation (this is an easy step, don’t worry).

  • Coding one or moreBeanDefinitionParserimplementations (this is where the real work is done).

  • Registering the above artifacts with Spring (this too is an easy step).

spring官方文档中42.1点介绍中有详细说明怎么自定义标签,简单的来说自定义标签就是:

  1. 编写一个xml架构来描述自定义的元素

  2. 编写一个自定义的handler实现NamespaceHandler接口

  3. 编写一个或者多个自定义的解析器实现BeanDefinitionParser接口 

  4. 注册到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 customnamespaceHandlerand 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 defaultEntityResolverthat 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 theNamespaceHandlerandBeanDefinitionParserclasses 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

0 0