dubbo-探索之旅(五)---spring中的schema

dubbo于spring框架结合,为了让dubbo配置与spring框架语义更贴近,所以会选择基于spring的XML扩展方式,来表示自己的特有的一些配置方式。所以这里先来看看spring中的schema(模式文档)。如何基于Spring来扩展自己的特有的标签。

完成一个自定义配置一般需要以下步骤:
1)设计配置属性和JavaBean
2)编写XSD文件
3)编写NamespaceHandler和BeanDefinitionParser完成解析工作
4)编写spring.handlers和spring.schemas串联起所有文件
5)应用

下面写一个实例来完成以上的步骤:
1)建一个JavaBean
public class Customer {
    private String id;
    /** 客户名称 */
    private String custName;
    /** 联系电话 */
    private String contactPhone;
    /** 联系邮箱 */
    private String email;

}


2)编写XSD文件
为上一步的JavaBean编写XSD文件,XSD是schema的定义文件,配置的输入和解析输出都是以XSD为契约。
<?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>  

关于xsd:schema的各个属性具体含义可以参考:http://www.w3school.com.cn/schema
<xsd:element name="customer">配置项节点的名称,在应用中会使用这个名字来引用配置。
<xsd:attribute/>配置bean的属性和类型
完成XSD文件后,把xsd存放在classpath下,一般都放在META-INF目录下。
3)编写NamespaceHandler和BeanDefinitionParser完成解析工作
NamespaceHandler:会根据XSD文件中的节点名称找到自己制定的BeanDefinitionParser,然后由BeanDefinitionParser完成具体的解析工作。spring提供了这个2个类的默认实现类:NamespaceHandlerSupport和AbstractSingleBeanDefinitionParser,最简单的就是直接继承这2个类。
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

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

registerBeanDefinitionParser("customer", new CustomerBeanDefinitionParser());方法就是把节点名称customer和解析类联系起来。配置项中的customer使用CustomerBeanDefinitionParser类解析配置。
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:获取配置文件中的值,bean.addPropertyValue:把属性值放到bean中。
4)编写spring.handlers和spring.schemas串联起所有文件
   Javabean,XSD文件,解析类都准备好了。现在是应该把他们串起来了。让他们能够协调工作。
   spring提供了两个配置文件来完成串起来的工作,这两个文件的地址必须是META-INF/spring.handlers和META-INF/spring.schemas,spring会默认去载入它们。

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

当使用到名为"http\://jishuaige.iteye.com/admin/blogs/schema/customer"的schema引用时,会通过com.jishuaige.schema.CustomerNamespaceHandlerSupport来完成解析

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

5)应用
现在简单的配置已经完成了,现在就应该应用到具体的实例中了。下面来配置一个spring bean,要基于我们自己的schema(模式文档)。以下是配置文件:
<?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"指定我们自定义的schema。xsi:schemaLocation指定xsd文件。
<customer:customer/>配置使用的实例。

下面我们来使用我们定义的这个BEAN:
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); 
Customer customer = (Customer) ctx.getBean("001");


上面就是我对于spring可扩展schema的提供自定义配置支持的一些理解和具体的操作。后面就在dubbo中看看实际的应用了。

猜你喜欢

转载自jishuaige.iteye.com/blog/2336000
今日推荐