7, Dubbo and spring fusion

The definition of beans in spring can be programmed, defined in the properties file, or defined in the xml file. The most used form is in the xml format, because the xml format has a good self-description and is easy to write and maintain. For xml document structure, data definition and format verification, DTD and Schema can be used. Before spring 2.0, DTD was used, and after spring 2.0, Schema was used . Using Schema makes it easier for spring to integrate with third parties, and third parties can provide simpler and easier-to-use personalized configuration methods. The specific knowledge of Xml Schema is not introduced here, but there is an important concept in Schema that must be mentioned. Namespace is used by spring to parse third-party custom configuration formats. In spring, aop, transaction The point is to give the third a good example of implementing your own custom configuration.

The class DefaultBeanDefinitionDocumentReader will read spring's xml configuration file as a document format to read this document, mainly explaining how dbbo's custom processor DubboNamespaceHandler converts dubbo's custom elements into bean definitions and registers them in spring's container.

 

public void init() {
        //Configure the <dubbo:application> tag parser 
        registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
        //Configure the <dubbo:module> tag parser 
        registerBeanDefinitionParser("module" , new DubboBeanDefinitionParser(ModuleConfig.class, true));
        //Configure <dubbo:registry> tag parser 
        registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
        //Configure <dubbo:monitor> tag parsing registerBeanDefinitionParser(" monitor 
        ", new DubboBeanDefinitionParser(MonitorConfig.class, true));
        //Configure the <dubbo:provider> tag parser 
        registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
        //配置<dubbo:consumer>标签解析器 
        registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
        //配置<dubbo:protocol>标签解析器
        registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));

There are so many BeanDefinitionParser registered in DubboNamespaceHandler to parse those xml element nodes defined by dubbo such as:

 

<dubbo:application name="dubbo-admin" />

<dubbo:registry address="${dubbo.registry.address}" check="false"file="false" />

<dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService" />

<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />

Each BeanDefinitionParser will convert the above xml element into the data structure BeanDefinition inside spring, and finally instantiate it into the corresponding bean when it is referenced, such as the <dubbo:application/> node to get the ApplicationConfig.

 

 

Of course, the default configuration is also possible, such as:

<dubbo:registry address="${dubbo.registry.address}" check="false" file="false" />

can also be configured as

<bean id=” registry”class=” com.alibaba.dubbo.config.RegistryConfig”/>

       <property name=” address” value=” ${dubbo.registry.address}”/>

       <property name=” check” value=” false”/>

<property name=” file” value=” false”/>

<bean/>

 

Using custom elements to parse is more concise, and can also block some specific implementation types. For example, you don’t need to know the class com.alibaba.dubbo.config.RegistryConfig, you only need to know the element of registration registry. Users can pass the document And the automatic prompt of schema in IDE can be easily configured.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326364995&siteId=291194637