Dubbo源码解析之:XML配置加载

本文深入解析Dubbo如何加载自定义XML配置,包括Spring自定义XML标签解析,Dubbo的xsd文件定义,spring.schemas与spring.handlers的作用,以及BeanDefinitionParser解析器的工作原理。详细阐述了从XML到Bean的加载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      如果阅读过前面文章的读者可以知道,我们使用的版本是2.5.4。GitHub链接https://github.com/apache/dubbo。选择tags的2.5.4下载。后面发现阿里将其重新开源并捐献给Apache后,做了一些改变,后面我们将使用tags2.6.5版本作为我们讲解版本。此文参考了https://juejin.im/post/5c1753b65188250850604ebe。

前言

在开始我们正式的讲解之前,这里先总览dubbo-demo中生产者的xml配置文件dubbo-demo-provider.xml,如果对xsd不了解的同学可以先看一下https://www.cnblogs.com/linjisong/p/3301096.html对xsd的介绍。

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>

    <!-- use multicast registry center to export service -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>

</beans>

不难猜想以 dubbo 开头的 xml 标签应该和 dubbo 密切相关。那么这些标签的用途是什么?又是如何被识别的呢? 带着这个疑问我们继续往下面看。

1.Spring 自定义 XML 标签解析

事实上Dubbo 中的自定义 XML 标签实际上是依赖于 Spring 解析自定义标签的功能实现的,我们结合 Spring 自定义 xml 标签实现的相关内容来聊聊 Dubbo 是如何定义并加载这些自定义标签的,当然这里不会对Spring解析自定义XML标签做过多解读,仅介绍下实现相关功能需要的文件,有需要的可以自己搜索相关内容。

  • 1.1定义 xsd 文件
    XSD(XML Schemas Definition) 即 XML 结构定义。我们通过 XSD 文件不仅可以定义新的元素和属性,同时也使用它对我们的 XML 文件规范进行约束。 在 Dubbo 项目中可以找类似实现:dubbo.xsd。(dubbo捐献给Apache以后对之前版本做了兼容,所以这里会有两个xsd)

  • 1.2spring.schemas
    该配置文件约定了自定义命名空间和 xsd 文件之间的映射关系,用于 spring 容器感知我们自定义的 xsd 文件位置。
    http\://dubbo.apache.org/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd
    http\://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/compat/dubbo.xsd

    注:这里同样是为了兼容之前版本的出现了两行。

  • 1.3spring.handlers
    该配置文件约定了自定义命名空间和 NamespaceHandler 类之间的映射关系。 NamespaceHandler 类用于注册自定义标签解析器。
    http\://dubbo.apache.org/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
    http\://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler

    注:这里同样是为了兼容之前版本的出现了两行。

  • 1.4命名空间处理器
    命名空间处理器主要用来注册 BeanDefinitionParser 解析器(dubbo中就对应上面 spring.handlers 文件中的 DubboNamespaceHandler)
    public class DubboNamespaceHandler extends NamespaceHandlerSupport {
    }

     

  • 1.5BeanDefinitionParser 解析器
    实现 BeanDefinitionParser 接口中的 parse 方法,用于自定义标签的解析(Dubbo 中对应 DubboBeanDefinitionParser 类)
    public class DubboBeanDefinitionParser implements BeanDefinitionParser {
        private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
        }
    }

    最后再给出 Spring 是如何从 XML 文件中解析并加载 Bean 的。不理解没关系可跳过

上图言尽于 handler.parse() 方法,如果你仔细看了上文,对 parse() 应该是有印象的。没错,1.5小节我们介绍了 DubboBeanDefinitionParser 类。该类有个方法就叫 parse()。那么这个 parse() 方法有什么用? Spring 是如何感知到我就要调用 DubboBeanDefinitionParser 类中的 parse() 方法的呢?我们带着这两个问题接着往下看。

2.Dubbo 解析自定义 XML 标签

上图最后一个类名为BeanDefinitionParserDelegate,那么我们从这里开始看起

BeanDefinitionParserDelegate.java
public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
    // 获取当前 element 的 namespaceURI
    // 比如 dubbo.xsd 中的为 http://dubbo.apache.org/schema/dubbo
    1、String namespaceUri = this.getNamespaceURI(ele);
    // 根据 URI 获取对应的 NamespaceHandler
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值