dubbo-exploration journey (5)---schema in spring

Dubbo is combined with the spring framework. In order to make the dubbo configuration closer to the semantics of the spring framework, the spring-based XML extension method is chosen to express some of its own unique configuration methods. So let's take a look at the schema (schema document) in spring first. How to extend your own unique tags based on Spring.

Completing a custom configuration generally requires the following steps:
1) Design configuration properties and JavaBeans
2) Write XSD files
3) Write NamespaceHandler and BeanDefinitionParser to complete the parsing work
4) Write spring.handlers and spring.schemas to concatenate all files
5) Apply

the following write An example to complete the above steps:
1) Create a JavaBean
public class Customer {
    private String id;
    /** client's name*/
    private String custName;
    /** contact number*/
    private String contactPhone;
    /** Contact email */
    private String email;

}


2) Write the XSD file Write the XSD file
for the JavaBean in the previous step. XSD is the definition file of the schema. The input and parsing output of the configuration are based on the XSD contract.
<?xml version="1.0" encoding="UTF-8"?>  
<xsd:schema   
    xmlns="http://jishuaige.iteye.com/admin/blogs/schema/customer"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"   
    xmlns:beans="http://www.springframework.org/schema/beans"  
    targetNamespace="http://jishuaige.iteye.com/admin/blogs/schema/customer"  
    elementFormDefault="qualified"   
    attributeFormDefault="unqualified">  
    <xsd:import namespace="http://www.springframework.org/schema/beans" />  
    <xsd:element name="customer">  
        <xsd:complexType>  
            <xsd:complexContent>  
                <xsd:extension base="beans:identifiedType">  
                    <xsd:attribute name="custName" type="xsd:string" />  
                    <xsd:attribute name="contactPhone" type="xsd:string" />
                    <xsd:attribute name="email" type="xsd:string" />  
                </xsd:extension>  
            </xsd:complexContent>  
        </xsd:complexType>  
    </xsd:element>  
</xsd:schema>  

For the specific meaning of each attribute of xsd:schema, please refer to: http://www.w3school.com.cn/schema
<xsd:element name="customer">The name of the configuration item node, which will be used in the application to refer to configuration.
<xsd:attribute/>Configure bean attributes and types
After completing XSD file, store the xsd in the classpath, generally in the META-INF directory.
3) Write NamespaceHandler and BeanDefinitionParser to complete the parsing work
NamespaceHandler: It will find its own BeanDefinitionParser according to the node name in the XSD file, and then the BeanDefinitionParser will complete the specific parsing work. Spring provides the default implementation classes of these two classes: NamespaceHandlerSupport and AbstractSingleBeanDefinitionParser. The simplest is to directly inherit these two classes.
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class CustomerNamespaceHandlerSupport extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("customer", new CustomerBeanDefinitionParser());
    }
}

registerBeanDefinitionParser("customer", new CustomerBeanDefinitionParser()); The method is to associate the node name customer with the parsing class. The customer in the configuration item uses the CustomerBeanDefinitionParser class to parse the configuration.
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

public class CustomerBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String custName = element.getAttribute("custName");
        String contactPhone = element.getAttribute("contactPhone");
        String email = element.getAttribute("email");
        String id = element.getAttribute("id");
        if (StringUtils.hasText(id)) {
            builder.addPropertyValue("id", id);
        }
        if (StringUtils.hasText(custName)) {
            builder.addPropertyValue("custName", custName);
        }
        if (StringUtils.hasText(contactPhone)) {
            builder.addPropertyValue("contactPhone", contactPhone);
        }
        if (StringUtils.hasText(email)) {
            builder.addPropertyValue("email", email);
        }
    }

    @Override
    protected Class<?> getBeanClass(Element element) {
        return Customer.class;
    }
}

element.getAttribute: Get the value in the configuration file, bean.addPropertyValue: Put the attribute value into the bean.
4) Write spring.handlers and spring.schemas to concatenate all files
   Javabeans, XSD files, and parsing classes are ready. Now it's time to string them together. Enable them to coordinate their work.
   Spring provides two configuration files to complete the string together. The addresses of these two files must be META-INF/spring.handlers and META-INF/spring.schemas, and spring will load them by default.

spring.handlers:
http\://jishuaige.iteye.com/admin/blogs/schema/customer=com.jishuaige.schema.CustomerNamespaceHandlerSupport

When using the name "http\://jishuaige.iteye.com/admin/blogs When the schema of /schema/customer" is referenced, the parsing of spring.schemas will be done through

com.jishuaige.schema.CustomerNamespaceHandlerSupport:
http\://jishuaige.iteye.com/admin/blogs/schema/customer.xsd=META-INF /customer.xsd

5) Application
Now that the simple configuration has been completed, it should now be applied to the specific instance. Let's configure a spring bean based on our own schema (schema document). Here is the configuration file:
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:customer="http://jishuaige.iteye.com/admin/blogs/schema/customer"  
    xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
http://jishuaige.iteye.com/admin/blogs/schema/customer http://jishuaige.iteye.com/admin/blogs/schema/customer.xsd">  
    <customer:customer id="001" custName="jishuaige" contactPhone="13600000000" email="[email protected]"/>  
</beans>  

xmlns:customer="http://jishuaige.iteye.com/admin/blogs/schema/customer" specifies our custom schema. xsi:schemaLocation specifies the xsd file.
The instance used by the <customer:customer/> configuration.

Let's use the BEAN we defined below:
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
Customer customer = (Customer) ctx.getBean("001");


The above are some of my understanding and specific operations of providing custom configuration support for spring extensible schema. Let's take a look at the actual application in dubbo later.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327013151&siteId=291194637
Recommended