前面已经对Dubbo跟Spring Boot的结合原理的分析了[https://www.jianshu.com/p/7048774803d5](Dubbo源码分析----Dubbo在Spring Boot启动时如何运行的),现在就是对前面几个注册的Bean进行分析
ServiceAnnotationBeanPostProcessor这个类用来处理Service标签的类。前面对于这个类有简单的介绍,这个类实现了spring的BeanDefinitionRegistryPostProcessor类,在Spring中这个类的是可以用来拓展的,当用户需要自定义注册bean的时候可以是实现这个接口的postProcessBeanDefinitionRegistry方法。这个接口的方法可以在BeanFactoryPostProcessor检测开始之前注册其他bean定义。查看ServiceAnnotationBeanPostProcessor的postProcessBeanDefinitionRegistry方法
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//packagesToScan(需要扫描的包参数)在创建这个Bean的时候当作参数传入了,这里会对参数进行去空格,并把配置文件中的配置的扫描参数也放到一起处理
Set<String> resolvedPackagesToScan = resolvePackagesToScan(packagesToScan);
//判断扫描列表是不是空
if (!CollectionUtils.isEmpty(resolvedPackagesToScan)) {
//注册包里面的Bean
registerServiceBeans(resolvedPackagesToScan, registry);
} else {
if (logger.isWarnEnabled()) {
logger.warn("packagesToScan is empty , ServiceBean registry will be ignored!");
}
}
}
主要的逻辑集中在registerServiceBeans方法中,这个方法就是查找需要扫面包里面的类贴有Service标签的类并注册成Bean。
private void registerServiceBeans(Set<String> packagesToScan, BeanDefinitionRegistry registry) {
// 定义扫描对象,该类继承了ClassPathBeanDefinitionScanner类,继承的原因主要是将ClassPathBeanDefinitionScanner中的protect类型的方法变成public级别的
//ClassPathBeanDefinitionScanner的作用就是扫面类路径中的Bean候选者
DubboClassPathBeanDefinitionScanner scanner =
new DubboClassPathBeanDefinitionScanner(registry, environment, resourceLoader);
//获取beanNameGenerator(用于为bean定义生成bean名称的策略接口)
BeanNameGenerator beanNameGenerator = resolveBeanNameGenerator(registry);
//设置BeanNameGenerator以用于检测到的scanner,这里可以理解为为注册的Bean生成BeanName用
scanner.setBeanNameGenerator(beanNameGenerator);
//将包含类型过滤器添加到包含列表的,就是一个过滤器,用来过滤贴了Service注解的对象。
scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
//遍历包
for (String packageToScan : packagesToScan) {
// Registers @Service Bean first
//注册指定包路径下的贴有Service的Bean
scanner.scan(packageToScan);
//查找@Service的所有BeanDefinitionHolders,无论@ComponentScan是否扫描。
Set<BeanDefinitionHolder> beanDefinitionHolders =
findServiceBeanDefinitionHolders(scanner, packageToScan, registry, beanNameGenerator);
//如果BeanDefinitionHolder不为空,对于BeanDefinitionHolder这个对象不知道可以去看看前面的文章
if (!CollectionUtils.isEmpty(beanDefinitionHolders)) {
//遍历然后注册Bean
for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
registerServiceBean(beanDefinitionHolder, registry, scanner);
}
if (logger.isInfoEnabled()) {
logger.info(beanDefinitionHolders.size() + " annotated Dubbo's @Service Components { " +
beanDefinitionHolders +
" } were scanned under package[" + packageToScan + "]");
}
} else {
if (logger.isWarnEnabled()) {
logger.warn("No Spring Bean annotating Dubbo's @Service was found under package["
+ packageToScan + "]");
}
}
}
}
上面的逻辑比较简单,就是对传入的包路径进行扫描,找到贴有@service标签的类,并会把生成的Bean相关信息封装到BeanDefinitionHolder中,然后对BeanDefinitionHolder集合进行遍历,进行自定义的注册,接下来就是分析自定义注册的部分registerServiceBean方法。
private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, BeanDefinitionRegistry registry,
DubboClassPathBeanDefinitionScanner scanner) {
//获取beanDefinitionHolder中的BeanClass
Class<?> beanClass = resolveClass(beanDefinitionHolder);
//beanClass中的Service注解
Service service = findAnnotation(beanClass, Service.class);
//从service注解中获取用户定义的interfaceClass如果没有定义则用beanClass的接口类中的第一个
Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
//获取BeanName
String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();
//使用BeanDefinitionBuilder创建Bean,bean的类型是ServiceBean,这里面会添加一些信息到Bean中比如ref,provider,monitor等信息
AbstractBeanDefinition serviceBeanDefinition =
buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName);
// 生成beanName
String beanName = generateServiceBeanName(service, interfaceClass, annotatedServiceBeanName);
//检查是否已经创建了相同名称跟类型的bean,没有就进行注册
if (scanner.checkCandidate(beanName, serviceBeanDefinition)) {
//注册Bean,这里可以参考前面的spring的源码分析
registry.registerBeanDefinition(beanName, serviceBeanDefinition);
if (logger.isInfoEnabled()) {
logger.info("The BeanDefinition[" + serviceBeanDefinition +
"] of ServiceBean has been registered with name : " + beanName);
}
} else {
if (logger.isWarnEnabled()) {
logger.warn("The Duplicated BeanDefinition[" + serviceBeanDefinition +
"] of ServiceBean[ bean name : " + beanName +
"] was be found , Did @DubboComponentScan scan to same package in many times?");
}
}
}
//------------创建Bean 的buildServiceBeanDefinition方法
private AbstractBeanDefinition buildServiceBeanDefinition(Service service, Class<?> interfaceClass,
String annotatedServiceBeanName) {
//注册Bean 的Class为ServiceBean.class
BeanDefinitionBuilder builder = rootBeanDefinition(ServiceBean.class);
//获取AbstractBeanDefinition,是为了后面添加属性
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
//从AbstractBeanDefinition中获取MutablePropertyValues对象(允许对属性进行简单操作,并提供构造函数以支持Map中的深层复制和构造)
MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol",
"interface", "interfaceName");
//添加自己的属性适配器
propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(service, environment, ignoreAttributeNames));
//将“ref”属性引用到带注释的@Service Bean
// References "ref" property to annotated-@Service Bean
addPropertyReference(builder, "ref", annotatedServiceBeanName);
// Set interface
builder.addPropertyValue("interface", interfaceClass.getName());
//设置标签中配置的providerName到provider属性,后面基本都是这种操作
String providerConfigBeanName = service.provider();
if (StringUtils.hasText(providerConfigBeanName)) {
addPropertyReference(builder, "provider", providerConfigBeanName);
}
........
}
这样上面的Service标签的处理就已经结束了,注意上面最后创建的Bean的是ServiceBean,这个类很重要。接下来就是对Reference标签的处理分析。