上篇我们介绍了Dubbbo整合Spring中的@EnableDubboConfig注解源码分析,地址如下
Dubbo源码解读-dubbo启动与Spring整合-优快云博客。
本文主要针对@ DubboComponentScan注解,从dubbo源码角度解析该注解功能。
接着说明,读Dubbo源码最好是先对Spring源码有一定的了解。如果大家需要,我也可以针对Spring框架做一系列源码的解读专栏。
不过不用担心,如果需要Spring的源码知识,文章中也会进行Spring源码铺垫介绍的。
如果内容中有没描述清楚的,或者大家在阅读源代码有疑问的,欢迎留言,看到就会及时回复。
为了更清楚的分析解释源码,源代码中部分不重要的内容可能会删减,保留重要内容方便大家理解。
主要内容
- @ DubboComponentScan解析
- ServiceAnnotationBeanPostProcessor源码解析
- ReferenceAnnotationBeanPostProcessor源码解析
@DubboComponentScan解析
该标签的主要职责其实就是负责注册两个BeanPostProcessor,分别是ServiceAnnotationBeanPostProcessor和ReferenceAnnotationBeanPostProcessor,分别负责处理@Service和@Reference注解。
具体流程如下:
-
注解上import了DubboComponentScanRegistrar类。
-
DubboComponentScanRegistrar实现了ImportBeanDefinitionRegistrar接口,会在Spring启动过程中,调用registerBeanDefinitions方法()
-
注册ServiceAnnotationBeanPostProcessor类BeanDefinition
-
注册ReferenceAnnotationBeanPostProcessor类BeanDefinition
Spring背景知识铺垫
Spring容器管理的类,不论是通过xml加载的还是注解加载的,都需要先进行收集成Beandefinetion对象,然后再根据该对象进程Bean 的实例化。实例化完成之后交给Spring管理。
- @Import注解对应的类会在,Spring启动过程中由 ConfigurationClassPostProcessor扫描,收集@Import注解
- 实现ImportBeanDefinitionRegistrar的类,在Spring启动过程中ConfigurationClassPostProcessor调用对应类中的registerBeanDefinitions()方法如DubboComponentScanRegistrar中的
源码流程
- 流程1
#Import标签倒入注册类
@Import(DubboComponentScanRegistrar.class)
public @interface DubboComponentScan {
}