Welcome to the public number [sharedCode] committed to mainstream middleware source analysis, you can contact me directly

preface

In the last section, we built a basic use case for Dubbo based on SpringBoot, which enables us to use the basic functions of Dubbo. Before Dubbo was open source, most people used Spring XML files to configure dubbo. With this background, it is necessary to talk about how Dubbo parses XML files. Of the generated proxy object.

Release notes

Springboot starter: while

Dubbo version: 2.6.2

DubboNamespaceHandler

This is the class that Dubbo’s Spring-based extension point uses to parse XML files

public class DubboNamespaceHandler extends NamespaceHandlerSupport {

    static {
        Version.checkDuplicate(DubboNamespaceHandler.class);
    }

    @Override
    public void init(a) {
        // Parses the 
       tag global configuration to configure the current application information, whether the application is a provider or a consumer
        registerBeanDefinitionParser("application".new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        // Parse the 
       tag module configuration to configure the current module information
        registerBeanDefinitionParser("module".new DubboBeanDefinitionParser(ModuleConfig.class, true));
        // Parse the 
       tag to configure the connection registry information
        registerBeanDefinitionParser("registry".new DubboBeanDefinitionParser(RegistryConfig.class, true));
        // Parse the 
       tag to configure information about the connection monitoring center
        registerBeanDefinitionParser("monitor".new DubboBeanDefinitionParser(MonitorConfig.class, true));
        // Resolve the 
       label. If the ProtocolConfig and ServiceConfig are not configured, the default value is used. It is optional
        registerBeanDefinitionParser("provider".new DubboBeanDefinitionParser(ProviderConfig.class, true));
        If the ReferenceConfig attribute is not configured, use the default value, which is optional
        registerBeanDefinitionParser("consumer".new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        // Parse the 
       tag protocol
        registerBeanDefinitionParser("protocol".new DubboBeanDefinitionParser(ProtocolConfig.class, true));
        // Parse the 
       tag to expose a service, defining the meta information of the service. A service can be exposed with multiple protocols, and a service can be registered with multiple registries
        registerBeanDefinitionParser("service".new DubboBeanDefinitionParser(ServiceBean.class, true));
        // Parse the 
       tag to create a remote service proxy, where a reference can point to multiple registries
        registerBeanDefinitionParser("reference".new DubboBeanDefinitionParser(ReferenceBean.class, false));
        // Parse the 
       tag
        registerBeanDefinitionParser("annotation".newAnnotationBeanDefinitionParser()); }}Copy the code

Spring parsing

 protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
        if(delegate.isDefaultNamespace(root)) {Dubbo NameSpace is not the default NameSpace
            NodeList nl = root.getChildNodes();
            for(int i = 0; i < nl.getLength(); ++i) {
                Node node = nl.item(i);
                if(node instanceof Element) {
                    Element ele = (Element)node;
                    if(delegate.isDefaultNamespace(ele)) {
                        this.parseDefaultElement(ele, delegate);
                    } else{ delegate.parseCustomElement(ele); }}}}else {
        // Use the proxy to parse the Element in the Dubbo configuration filedelegate.parseCustomElement(root); }}public BeanDefinition parseCustomElement(Element ele, BeanDefinition containingBd) {
        // Get namespaceURI from the element
        String namespaceUri = this.getNamespaceURI(ele);
        / / as ele: name of dubbo: application, the namespaceUri = http://code.alibabatech.com/schema/dubbo
        // Get the NamespaceHandler from NameSpaceURi. Dubbo is the DubboNameSpaceHandler
        NamespaceHandler handler = this.readerContext.getNamespaceHandlerResolver()
        .resolve(namespaceUri);
        if(handler == null) {
            this.error("Unable to locate Spring NamespaceHandler for XML schema namespace [" + namespaceUri + "]", ele);
            return null;
        } else {
            / / DubboBeanDefinitionParser parse parsing
            return handler.parse(ele, new ParserContext(this.readerContext, this, containingBd)); }}Copy the code

NamespaceHandlerSupport

Spring’s custom schema tag parsing is done by writing a class that inherits from NamespaceHandlerSupport and implementing the init method. In the init method, the parser is unregistered, and the XML parser is then parsed using the convention key in the map.

RegisterBeanDefinitionParser is a map

Spring container at the time of launch, there was a custom tag, go through the label name registerBeanDefinitionParser looking for labels in the parser, the parse method then calls the tag parser, get BeanDefinition, finally instantiation bean.

So dubbo’s XML parsing is based on Spring’s NamespaceHandler mechanism, then parsed through the tag’s corresponding parser, and finally instantiated bean

Welcome to the public number [sharedCode] committed to mainstream middleware source analysis, you can contact me directly