Spring提供了可扩展Schema的支持,完成一个自定义配置一般需要以下步骤:

  • 设计配置属性和JavaBean
  • 编写XSD文件
  • 编写NamespaceHandler和BeanDefinitionParser完成解析工作
  • 编写spring.handlers和spring.schemas串联起所有部件
  • 在Bean文件中应用

dubbo中所有dubbo的标签,都统一用DubboBeanDefinitionParser进行解析,基于一对一属性映射,将XML标签解析为Bean对象。
下面我们就用dubbo为例来看如何实现对Spring配置的标签扩展。

 

1、设计配置属性和JavaBean

我们首先当然得设计好配置项,并通过JavaBean来建模。

以Dubbo的ServiceBean为例,这里定义的了dubbo每个服务的信息。

 

ServiceBean类的关系图如下图:

 

2、编写XSD文件

XSD文件所在的位置在dubbo-config-spring项目中,如下图:

以后面要用到的dubbo:service为例,:

<xsd:element name="service" type="serviceType">
        <xsd:annotation>
            <xsd:documentation><![CDATA[ Export service config ]]></xsd:documentation>
        </xsd:annotation>
</xsd:element>

serviceType类型的定义如下:

    <xsd:complexType name="serviceType">
        <xsd:complexContent>
            <xsd:extension base="abstractServiceType">
                <xsd:choice minOccurs="0" maxOccurs="unbounded">
                    <xsd:element ref="method" minOccurs="0" maxOccurs="unbounded" />
                    <xsd:element ref="parameter" minOccurs="0" maxOccurs="unbounded" />
                    <xsd:element ref="beans:property" minOccurs="0" maxOccurs="unbounded" />
                </xsd:choice>
                <xsd:attribute name="interface" type="xsd:token" use="required">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[ Defines the interface to advertise for this service in the service registry. ]]></xsd:documentation>
                        <xsd:appinfo>
                            <tool:annotation>
                                <tool:expected-type type="java.lang.Class"/>
                            </tool:annotation>
                        </xsd:appinfo>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="ref" type="xsd:string" use="optional">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[ The service implementation instance bean id. ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="class" type="xsd:string" use="optional">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[ The service implementation class name. ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="path" type="xsd:string" use="optional">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[ The service path. ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="provider" type="xsd:string" use="optional">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[ Deprecated. Replace to protocol. ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:attribute name="generic" type="xsd:string" use="optional">
                    <xsd:annotation>
                        <xsd:documentation><![CDATA[ Generic service. ]]></xsd:documentation>
                    </xsd:annotation>
                </xsd:attribute>
                <xsd:anyAttribute namespace="##other" processContents="lax" />
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

比如其中的 <xsd:attribute name="ref" type="xsd:string" use="optional"> 对应的 string类型的配置项 ref。

 

 

3、编写NamespaceHandler和BeanDefinitionParser完成解析工作

要完成解析工作,会用到NamespaceHandler和BeanDefinitionParser这两个概念。具体说来NamespaceHandler会根据schema和节点名找到某个BeanDefinitionParser,然后由BeanDefinitionParser完成具体的解析工作。因此需要分别完成NamespaceHandler和BeanDefinitionParser的实现类,Spring提供了默认实现类NamespaceHandlerSupport和AbstractSingleBeanDefinitionParser,简单的方式就是去继承这两个类。

如上图,dubbo的 NamespaceHandler 文件在 com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler  。

Handler 定义了每个配置节解析的对象,如下图代码。

DubboBeanDefinitionParser 定义也在这个目录下。

 

4、编写spring.handlers和spring.schemas串联起所有部件

上面几个步骤走下来会发现开发好的handler与xsd还没法让应用感知到,就这样放上去是没法把前面做的工作纳入体系中的,spring提供了spring.handlers和spring.schemas这两个配置文件来完成这项工作,这两个文件需要我们自己编写并放入META-INF文件夹中,这两个文件的地址必须是META-INF/spring.handlers和META-INF/spring.schemas,spring会默认去载入它们。

这两个文件,在dubbo项目中,是在dubbo-config-spring项目中,如下图:

 

spring.handlers 文件的内容如下:

http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler

以上表示当使用到名为"http://code.alibabatech.com/schema/dubbo"的schema引用时,会通过com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler 来完成解析

spring.schemas 文件的内容如下:

http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd

以上标示载入的xsd文件的位置。

 

5、在Bean文件中应用

Dubbo的Spring的配置文件来自哪里,请参考这篇文章:

Dubbo 通过Spring 配置具体启动服务(http://www.cnblogs.com/ghj1976/p/5320195.html

以dubbo-demo-provider为例,它的Spring配置文件 dubbo-demo-provider.xml内容如下:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
   
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl" />
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
   
</beans>

其中:

  • xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 是用来指定自定义schema,
  • xsi:schemaLocation用来指定xsd文件。
  • <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> 是一个具体的自定义配置使用实例。

 

参考:

基于Spring可扩展Schema提供自定义配置支持
http://blog.csdn.net/cutesource/article/details/5864562

Dubbo中对Spring配置标签扩展的更多相关文章

  1. Spring 配置标签——util标签

    Spring 配置标签——util标签     一.配置applicationContext.xml <beans xmlns="http://www.springframework. ...

  2. 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean

    [spring以及第三方jar的案例]在spring中的aop相关配置的标签,线程池相关配置的标签,都是基于该种方式实现的.包括dubbo的配置标签都是基于该方式实现的.[一]原理 ===>sp ...

  3. 缘起 Dubbo ,讲讲 Spring XML Schema 扩展机制

    背景 在 Dubbo 中,可以使用 XML 配置相关信息,也可以用来引入服务或者导出服务.配置完成,启动工程,Spring 会读取配置文件,生成注入 相关 Bean.那 Dubbo 如何实现自定义 X ...

  4. javaEE中的spring配置笔记

    0 JavaEE的工程目录 0.1 WebContent     项目的主目录,在eclipse新建工程时可以自己命名,部署时会把该文件夹的内容发布到tomcat的webapps里. 该目录下可以建立 ...

  5. Spring Boot2.0+中,自定义配置类扩展springMVC的功能

    在spring boot1.0+,我们可以使用WebMvcConfigurerAdapter来扩展springMVC的功能,其中自定义的拦截器并不会拦截静态资源(js.css等). @Configur ...

  6. maven的pom.xml配置文件中常用的配置标签解析(2018-03-13)

    来自:https://www.cnblogs.com/Nick-Hu/p/7288198.html 拿过来记录下 <project xmlns="http://maven.apache ...

  7. dubbo的几种配置方式(转)

    昨天刚接触公司dubbo,发现公司中项目里面的spring-dubbo-privider的dubbo中<dubbo:application name=""/>和< ...

  8. [SSH 3]以网上商城项目浅谈spring配置

    导读:在做ITOO项目的时候,就用到了容器+反射,从而运用了依赖注入和依赖查找.如果看过WCF端的配置文件,那么对于这个spring的配置就很容易理解.本篇博客,是对于自己做的一个小项目中所运用到的s ...

  9. Spring配置扫描mybatis的mapper文件注意:

    一般会将不业务的mapper文件放到不同的包中: spring配置扫描就需要配置下面的方式(两个*): <!-- mybatis文件配置,扫描所有mapper文件 --> <bean ...

随机推荐

  1. linux包之coreutils之du和df命令

    [root@localhost ~]# rpm -qf /usr/bin/ducoreutils-8.4-31.el6.x86_64[root@localhost ~]# rpm -qf /bin/d ...

  2. 树莓派 2 win 10 IOT

    Setting up Windows 10 for IoT on your Raspberry Pi This week at the BUILD conference in San Francisc ...

  3. 【转】Php+ajax+jsonp解决ajax跨域问题

    首先:jsonp是json用来跨域的一个东西. 原理是通过script标签的跨域特性来绕过同源策略. 发送端: $.ajax({ type : "post", url : &quo ...

  4. windows下android开发环境搭建

    JDK的安装和Java环境变量的设置 1 JDK下载地址 JDK下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.h ...

  5. ExtJs学习笔记之ComboBox组件

    ComboBox组件 (1)ComboBox控件支持自动完成.远程加载.和许多其他特性. (2)ComboBox就像是传统的HTML文本 <input> 域和 <select> ...

  6. sklearn小知识

    特征缩放:# 为了追求机器学习和最优化算法的最佳性能,我们将特征缩放 from sklearn.preprocessing import StandardScaler sc = StandardSca ...

  7. java 金额计算,商业计算 double不精确问题 BigDecimal,Double保留两位小数方法

    解决办法================== http://blog.javaxxz.com/?p=763 一提到Java里面的商业计算,我们都知道不能用float和double,因为他们无法 进行精 ...

  8. Linux环境下vsftpd参数配置

    很久之前就用过vsftpd,但总是忘了参数该如何配置,今天特地有搜索了一遍,把配置方法整理如下: (1)检查是否已安装vsftpd #rpm -qa | grep vsftpd vsftpd--.el ...

  9. HTML <div> 和 <span>

    可以通过 <div> 和 <span> 将 HTML 元素组合起来. HTML 块元素 大多数 HTML 元素被定义为块级元素或内联元素. 编者注:“块级元素”译为 block ...

  10. LINQ学习入门教程(一)

    LINQ 查询简介       Linq 是一跨各种数据源和数据格式的数据模型:它在查询是,始终是把它作为一种对象来操作,可以使用基本相同的编码模型查询和数据的转换XML,SQL,ADO数据等: Li ...