Dubbo中的xml实例注入解析

1.dubbo实现对xml配置文件解析主要有META-INF下的这三个配置文件,dubbo.xsd用于定义xml配置文件的格式;spring.handlers配置了xml的解析类;spring.schemas配置了xml域名上下文与xsd文件的对应关系

2.spring.handlers,com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler为处理解析XML的类

3.spring.schemas,配置了XMLschemal域名与xsd文件的对应关系

4.com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler处理类,需要继承org.springframework.beans.factory.xml.NamespaceHandlerSupport类,在init方法中注册每种节点的解析类(如 <dubbo:application>)

5.com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser解析类,需要实现org.springframework.beans.factory.xml.BeanDefinitionParser接口,spring在启动解析XML时,会把解析到的对应名称的节点作为参数回调parse方法,在此方法中对节点进行解析,以及注入对应的Bean实例即可。

6.截取Dubbo注入Bean的某个代码片段

public BeanDefinition parse(Element element, ParserContext parserContext) {
    return parse(element, parserContext, this.beanClass, this.required);
}

private static BeanDefinition parse(Element element, ParserContext parserContext, Class<?> beanClass, boolean required) {
    RootBeanDefinition beanDefinition = new RootBeanDefinition();
    beanDefinition.setBeanClass(beanClass);
    beanDefinition.setLazyInit(false);
    String id = element.getAttribute("id");
    String className;
    int len$;
    if ((id == null || id.length() == 0) && required) {
        className = element.getAttribute("name");
        if (className == null || className.length() == 0) {
            if (ProtocolConfig.class.equals(beanClass)) {
                className = "dubbo";
            } else {
                className = element.getAttribute("interface");
            }
        }

        if (className == null || className.length() == 0) {
            className = beanClass.getName();
        }

        id = className;

        for(len$ = 2; parserContext.getRegistry().containsBeanDefinition(id); id = className + len$++) {
            ;
        }
    }

    if (id != null && id.length() > 0) {
        if (parserContext.getRegistry().containsBeanDefinition(id)) {
            throw new IllegalStateException("Duplicate spring bean id " + id);
        }

        parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
        beanDefinition.getPropertyValues().addPropertyValue("id", id);
    }

}

猜你喜欢

转载自blog.csdn.net/MCC_MCC_MCC/article/details/81566048