目录
Dubbo的XML的使用方式是如何与Spring的整合原理
dubbo的Service注入spring主要是org.apache.dubbo.config.spring.beans.factory.annotation.ServiceClassPostProcessor这个类
ServiceClassPostProcessor
可以看下这个类的定义:
public class ServiceClassPostProcessor implements BeanDefinitionRegistryPostProcessor, EnvironmentAware,
ResourceLoaderAware, BeanClassLoaderAware {
private final static List<Class<? extends Annotation>> serviceAnnotationTypes = asList(
// @since 2.7.7 Add the @DubboService , the issue : https://github.com/apache/dubbo/issues/6007
DubboService.class,
// @since 2.7.0 the substitute @com.alibaba.dubbo.config.annotation.Service
Service.class,
// @since 2.7.3 Add the compatibility for legacy Dubbo's @Service , the issue : https://github.com/apache/dubbo/issues/4330
com.alibaba.dubbo.config.annotation.Service.class
);
.................
}
实现了BeanDefinitionRegistryPostProcessor类
BeanDefinitionRegistryPostProcessor这个类提供了动态注册Bean到容器中的能力, 具体的可以参考 https://blog.csdn.net/ztchun/article/details/90814135 这篇博客。
Spring 在启动的时候会调用AbstractApplicationContext类的refresh方法, 我把refresh方法的代码复制在下面:
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 这里会调用容器中实现了BeanFactoryPostProcessor接口的所有bean的postProcessBeanFactory方法,
//而BeanDefinitionRegistryPostProcessor就是实现了这个接口, 所以上面的ServiceClassPostProcessor类的方法会在这一步调用
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
在上面的注释中已经说明了在spring的启动的时候会调用容器中的ServiceClassPostProcessor
的实例方法来注册所有的dubbo的Service服务提供者到容器中;
ServiceClassPostProcessor的bean实例是如何提前注入到容器中的。
目前dubbo集成spring的方式有两种, 一种是使用beans.xml
文件的方式, 还有一种是使用springboot的全部注解的方式, 其实两种方式原理是一样的。
我复制一下官网的dubbo的spring使用示例:
图片中使用了自定义的标签<dubbo:....... >
的方式配置一些dubbo的属性, 关于spring的自定义标签的知识又是一大堆,这里只是简单的说下:
图上是dubbo源码中的文件, 文件配置了dubbo自定义标签的解析类是:org.apache.dubbo.config.spring.schema.DubboNamespaceHandler
dubbo自定义标签的解析类DubboNamespaceHandler 的定义如下:
图中定义了每个标签对应的解析类都是DubboBeanDefinitionParser
, 只是传入的参数各不相同,DubboBeanDefinitionParser
类会把传入的第一个参数生成BeanDefinition放入spring容器中. 这里第一次产生了spring和dubbo的交集。
其中 service标签会的解析成ServiceBean类放入spring容器中, reference标签会解析成ReferenceBean类放入容器中,还有annotation标签的解析对象AnnotationBeanDefinitionParser
类会扫描项目中的@Service 和@Reference注解信息,生成对应的bean到容器中。
AnnotationBeanDefinitionParser
类源码
它的源码有两个方法, 其中 registerCommonBeans方法内部会注册一堆的时间监听器和BeanPostProcessor到容器中, 注册的bean如下:
* ReferenceAnnotationBeanPostProcessor
* DubboConfigDefaultPropertyValueBeanPostProcessor
* DubboConfigAliasPostProcessor
* DubboLifecycleComponentApplicationListener
* DubboBootstrapApplicationListener
这五个bean都是十分重要的, 会在之后的文章中讲到。
getBeanClass方法返回一个ServiceAnnotationBeanPostProcessor类, 这个类继承自ServiceClassPostProcessor类, 所以ServiceClassPostProcessor类就加入的容器中了。
之后的文章会详细说明ServiceAnnotationBeanPostProcessor类的原理和Service注解的作用机制。 https://blog.csdn.net/leisurelen/article/details/107019516