1. To understand how Dubbo interprets tags, it is important to understand how Spring handles custom tags, because Dubbo tags can be a case of Spring custom tags.

2. Spring uses two interfaces to parse custom tags: NamespaceHandler and BeanDefinitionParser; NamespaceHandler handles namespace processing, while BeanDefinitionParser handles Bean parsing;

We can implement NamespaceHandler interface when we customize tags, but more abstract classes inherit from NamespaceHandler: NamespaceHandlerSupport, then write a Spring. handlers file in the META-INF directory of the classpath that defines the URL for the namespace and the mapping of the namespace handling classes.

For example, Dubbo’s Spring. handlers file (/ is escaped) : http\://dubbo.apache.org/schema/dubbo=org.apache.dubbo.config.spring.schema.DubboNamespaceHandler

The Spring-handlers file of the Classpath is loaded at initialization, and the namespace URL and namespace handling classes are mapped to the Map. It looks for the Namespace handler class in this Map and uses this to use this custom handler class to parse labels;

5. Similarly, Dubbo’s namespace handler class is DubboNamespaceHandler, which implements the NamespaceHandlerSupport interface to handle namespaces, and then registers all tags with their respective parsers when the class is initialized. And Dubbo parsing class DubboBeanDefinitionParser also implements the BeanDefinitionParser interface;



XML configuration for Dubbo: Implementation of Schema and XML extension mechanism based on Spring. With this mechanism, we can write our own Schema and configure beans based on custom tags of our own Schema. There are several steps to using Spring’s XML extension mechanism:

  1. Define the Schema (write the.xsd file)
  2. Define javabeans
  3. Write NamespaceHandler and BeanDefinitionParser to parse the Schema
  4. Write spring. Handlers and spring. Schemas to parse the components in tandem
  5. Apply the configuration in an XML file



To define the Schema

Dubbo Schema definition is embodied in Dubbo. XSD file, the file is located in Dubbo – config – spring modules, the basic knowledge of XSD can refer to: www.w3school.com.cn/schema/inde…

Define javabeans

The Dubbo – config-API submodule defines the Javabeans corresponding to all dubbo tags. The attributes in the Javabeans correspond to each configuration item of the tag. In the org.apache.dubo. config package.

Parse the Schema

Dubbo service framework Schema analysis through DubboNamespaceHandler and DubboBeanDefinitionParser implementation. DubboNamespaceHandler extends Spring’s NamespaceHandlerSupport by overriding its init() method to register the corresponding parser for each tag:





And the BeanDefinitionParser DubboBeanDefinitionParser has realized the Spring, by rewriting the parse () method will tag resolves to corresponding JavaBean:



Serial parts

So how does Spring know to use the DubboNamespaceHandler to parse Dubbo tags? This is done by writing the spring.handlers file. Handlers are as follows:

http\://dubbo.apache.org/schema/dubbo=org.apache.dubbo.config.spring.schema.DubboNamespaceHandlerCopy the code

Spring then verifies the format of the application XML configuration file by learning from the spring.schemas file that the Schema for the Dubbo label is Dubo.xsd. The contents of the spring.schemas file are as follows:

http\://dubbo.apache.org/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsdCopy the code

Application configuration

With the above steps, the Dubbo service framework completes the tag parsing function, and the user can configure THE XML in the Dubbo. XSD format in the application.


Reference article:

Github.com/cyfonly/dub…