Dubbo学习摘录(一)

Dubbo基于自定义配置,实现自己的Bean。
通常,完成一个自定义配置需要以下步骤:

  • 设计配置属性和JavaBean;
  • 编写XSD文件;
  • 编写NamespaceHandler和BeanDefinitionParser完成解析工作;
  • 编写spring.handlers和spring.schemas串联起所有部件;
  • 在Bean文件中应用。

下面是一个小Demo,
1)设计配置属性和JavaBean
首先当然得设计好配置项,并通过 JavaBean 来建模,本例中需要配置 People 实体,配
置属性 name 和 age(id 是默认需要的)

public class People {
        private String id;
        private String name;
        private Integer age;
    }

2)编写 XSD 文件
为上一步设计好的配置项编写 XSD 文件,XSD 是 schema 的定义文件,配置的输入和解
析输出都是以 XSD 为契约,本例中 XSD 如下:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
        xmlns="http://blog.csdn.net/cutesource/schema/people" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://blog.csdn.net/cutesource/schema/people" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="people">
    <xsd:complexType> <xsd:complexContent>
        <xsd:extension base="beans:identifiedType"> <xsd:attribute name="name" type="xsd:string" /> <xsd:attribute name="age" type="xsd:int" />
        </xsd:extension> </xsd:complexContent>
    </xsd:complexType> </xsd:element>
</xsd:schema>

xsd:schema 的各个属性具体含义参考 http://www.w3school.com.cn/schema/schema_schema.asp

对应着配置项节点的名称,因此在应用中会用 people 作为 节点名来引用这个配置
对应着配置项people的两个属性名,因此在应用中可以配置name和 age 两个属性,分别是 string 和 int 类型
完成后需把 xsd 存放在 classpath 下,一般都放在 META-INF 目录下(本例就放在这个目录下)。

3)编写 NamespaceHandler 和 BeanDefinitionParser 完成解析工作
下面需要完成解析工作,会用到 NamespaceHandler 和 BeanDefinitionParser 这两个概念。
具体说来 NamespaceHandler 会根据 schema 和节点名找到某个 BeanDefinitionParser,然后由 BeanDefinitionParser 完成具体的解析工作。因此需要分别完成 NamespaceHandler 和 BeanDefinitionParser 的实现类,Spring 供了默认实现类 NamespaceHandlerSupport 和 AbstractSingleBeanDefinitionParser,简单的方式就是去继承这两个类。本例就是采取这种方式:

import org.springframework.beans.factory.xml.NamespaceHandlerSupport; 
public class MyNamespaceHandler extends NamespaceHandlerSupport {
    public void init() {
        registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser()); 
    } 
}

其中 registerBeanDefinitionParser("people", new PeopleBeanDefinitionParser());就是用来 把节点名和解析类联系起来,在配置中引用 people 配置项时,就会用
PeopleBeanDefinitionParser 来解析配置。PeopleBeanDefinitionParser 就是本例中的解析类:

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 PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    protected Class getBeanClass(Element element) {
        return People.class;
    }

    protected void doParse(Element element, BeanDefinitionBuilder bean) {
        String name = element.getAttribute("name");
        String age = element.getAttribute("age");
        String id = element.getAttribute("id");
        if (StringUtils.hasText(id)) {
            bean.addPropertyValue("id", id);
        }
        if (StringUtils.hasText(name)) {
            bean.addPropertyValue("name", name);
        }
        if (StringUtils.hasText(age)) {
            bean.addPropertyValue("age", Integer.valueOf(age));
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/jenkov/p/dubbo_learning_001.html