Extension of Spring configuration tags in Dubbo

Spring provides support for extensible Schema, and completing a custom configuration generally requires the following steps:

  • Designing Configuration Properties and JavaBeans
  • Write XSD file
  • Write NamespaceHandler and BeanDefinitionParser to complete the parsing work
  • Write spring.handlers and spring.schemas to concatenate all the parts
  • Apply in bean file

All dubbo tags in dubbo are parsed uniformly with DubboBeanDefinitionParser, and based on one-to-one attribute mapping, XML tags are parsed into Bean objects. 
Let's take dubbo as an example to see how to implement label extension for Spring configuration.

 

1. Design configuration properties and JavaBeans

Of course, we first have to design configuration items and model them through JavaBeans.

Taking Dubbo's ServiceBean as an example, the information of each service of dubbo is defined here.

image

 

The relationship diagram of the ServiceBean class is as follows:

 

image

2. Write the XSD file

The location of the XSD file is in the dubbo-config-spring project, as shown below:

image

Take the dubbo:service to be used later as an example:

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

The definition of the serviceType type is as follows:

    <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>

For example, <xsd:attribute name="ref" type="xsd:string" use="optional"> corresponds to the string type configuration item ref.

 

 

3. Write NamespaceHandler and BeanDefinitionParser to complete the parsing work

To complete the parsing work, the concepts of NamespaceHandler and BeanDefinitionParser are used. Specifically, NamespaceHandler will find a BeanDefinitionParser according to the schema and node name, and then the BeanDefinitionParser will complete the specific parsing work. Therefore, it is necessary to complete the implementation classes of NamespaceHandler and BeanDefinitionParser respectively. Spring provides default implementation classes NamespaceHandlerSupport and AbstractSingleBeanDefinitionParser. The simple way is to inherit these two classes.

image

As shown above, dubbo's NamespaceHandler file is in com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler.

Handler defines the object parsed by each configuration section, as shown in the code below.

image

DubboBeanDefinitionParser definitions are also in this directory.

 

4. Write spring.handlers and spring.schemas to connect all the components

After walking down the above steps, you will find that the developed handler and xsd cannot be perceived by the application, so it is impossible to incorporate the previous work into the system. Spring provides spring.handlers and spring.schemas. Two configuration files to complete this work, these two files need to be written by ourselves and put into the META-INF folder, the addresses of these two files must be META-INF/spring.handlers and META-INF/spring. schemas, spring will load them by default.

These two files, in the dubbo project, are in the dubbo-config-spring project, as shown below:

image

 

The contents of the spring.handlers file are as follows:

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

The above means that when a schema reference named "http://code.alibabatech.com/schema/dubbo" is used, the parsing will be done through com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler 

The content of the spring.schemas file is as follows:

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

The above indicates the location of the loaded xsd file.

 

5. Apply in the Bean file

Where does Dubbo's Spring configuration file come from, please refer to this article:

Dubbo configures specific startup services through Spring ( http://www.cnblogs.com/ghj1976/p/5320195.html )

Taking dubbo-demo-provider as an example, the content of its Spring configuration file dubbo-demo-provider.xml is as follows:

<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>

in:

  • xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo"  is used to specify a custom schema,
  • xsi:schemaLocation is used to specify the xsd file.
  • <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> is a specific example of custom configuration usage.

 

refer to:

Provide custom configuration support based on Spring Extensible Schema 
http://blog.csdn.net/cutesource/article/details/5864562

 

 
 Reprinted: http://www.cnblogs.com/ghj1976/p/5379332.html#commentform

Spring provides support for extensible Schema, and completing a custom configuration generally requires the following steps:

  • Designing Configuration Properties and JavaBeans
  • Write XSD file
  • Write NamespaceHandler and BeanDefinitionParser to complete the parsing work
  • Write spring.handlers and spring.schemas to concatenate all the parts
  • Apply in bean file

All dubbo tags in dubbo are parsed uniformly with DubboBeanDefinitionParser, and based on one-to-one attribute mapping, XML tags are parsed into Bean objects. 
Let's take dubbo as an example to see how to implement label extension for Spring configuration.

 

1. Design configuration properties and JavaBeans

Of course, we first have to design configuration items and model them through JavaBeans.

Taking Dubbo's ServiceBean as an example, the information of each service of dubbo is defined here.

image

 

The relationship diagram of the ServiceBean class is as follows:

 

image

2. Write the XSD file

The location of the XSD file is in the dubbo-config-spring project, as shown below:

image

Take the dubbo:service to be used later as an example:

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

The definition of the serviceType type is as follows:

    <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>

For example, <xsd:attribute name="ref" type="xsd:string" use="optional"> corresponds to the string type configuration item ref.

 

 

3. Write NamespaceHandler and BeanDefinitionParser to complete the parsing work

To complete the parsing work, the concepts of NamespaceHandler and BeanDefinitionParser are used. Specifically, NamespaceHandler will find a BeanDefinitionParser according to the schema and node name, and then the BeanDefinitionParser will complete the specific parsing work. Therefore, it is necessary to complete the implementation classes of NamespaceHandler and BeanDefinitionParser respectively. Spring provides default implementation classes NamespaceHandlerSupport and AbstractSingleBeanDefinitionParser. The simple way is to inherit these two classes.

image

As shown above, dubbo's NamespaceHandler file is in com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler.

Handler defines the object parsed by each configuration section, as shown in the code below.

image

DubboBeanDefinitionParser definitions are also in this directory.

 

4. Write spring.handlers and spring.schemas to connect all the components

After walking down the above steps, you will find that the developed handler and xsd cannot be perceived by the application, so it is impossible to incorporate the previous work into the system. Spring provides spring.handlers and spring.schemas. Two configuration files to complete this work, these two files need to be written by ourselves and put into the META-INF folder, the addresses of these two files must be META-INF/spring.handlers and META-INF/spring. schemas, spring will load them by default.

These two files, in the dubbo project, are in the dubbo-config-spring project, as shown below:

image

 

The contents of the spring.handlers file are as follows:

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

The above means that when a schema reference named "http://code.alibabatech.com/schema/dubbo" is used, the parsing will be done through com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler 

The content of the spring.schemas file is as follows:

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

The above indicates the location of the loaded xsd file.

 

5. Apply in the Bean file

Where does Dubbo's Spring configuration file come from, please refer to this article:

Dubbo configures specific startup services through Spring ( http://www.cnblogs.com/ghj1976/p/5320195.html )

Taking dubbo-demo-provider as an example, the content of its Spring configuration file dubbo-demo-provider.xml is as follows:

<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>

in:

  • xmlns:dubbo=" http://code.alibabatech.com/schema/dubbo"  is used to specify a custom schema,
  • xsi:schemaLocation is used to specify the xsd file.
  • <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" /> is a specific example of custom configuration usage.

 

refer to:

Provide custom configuration support based on Spring Extensible Schema 
http://blog.csdn.net/cutesource/article/details/5864562

 

Guess you like

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