dubbo@service@dubboService解析
⽼规矩,从框架的使⽤⽅式⼊⼿分析,类似mybatis,dubbo也有个扫描服务的注解:
org.t.annotation.DubboComponentScan
同样地,配套⼀个@Import:org.t.annotation.DubboComponentScanRegistrar,作⽤就是把BeanDefinitionRegistry交给你全权处理,
对应这⾥就是构建⼀个org.fig.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor的BeanDefinition,然后加⼊BeanDefinitionRegistry。
这是个BeanDefinitionRegistry后处理bean,主要逻辑在⽗类
org.fig.spring.beans.factory.annotation.ServiceClassPostProcessor,最新版本直接就⽤了⽗类。
在如下dubbo springboot⾃动配置包中有个⾃动配置类,也是通过该后置bean解析@dubboService类的,不同的是,该⾃动配置的⽅式是从yaml获取服务类所在的包路径:
⾃动配置类org.apache.dubbo.spring.boot.autoconfigure.DubboAutoConfiguration:
对应yaml⽂件内容如下,这种⽅式配置的好处是可以把该配置项放到配置中⼼统⼀管理:
下⾯我们具体看下这个类:
实现了接⼝BeanDefinitionRegistryPostProcessor,对BeanDefinitionRegistry进⾏后处理,主要逻辑在
org.fig.spring.beans.factory.annotation.ServiceClassPostProcessor#registerServiceBeans⽅法:
private void registerServiceBeans(Set<String> packagesToScan, BeanDefinitionRegistry registry) {
DubboClassPathBeanDefinitionScanner scanner =
new DubboClassPathBeanDefinitionScanner(registry, environment, resourceLoader);
BeanNameGenerator beanNameGenerator = resolveBeanNameGenerator(registry);
scanner.setBeanNameGenerator(beanNameGenerator);
// refactor @since 2.7.7
serviceAnnotationTypes.forEach(annotationType -> {
// 设置扫描的包下被指定注解标注的类是需要处理的serviceAnnotationTypes包含如下⼏个注解
// org.fig.annotation.DubboService,2.7.7版本后直接⽤这个注解暴露服务
// org.fig.annotation.Service,@Deprecated
// com.fig.annotation.Service,@Deprecated
scanner.addIncludeFilter(new AnnotationTypeFilter(annotationType));
});
for (String packageToScan : packagesToScan) {
// 注册注解标注的类BeanDefinition
// Registers @Service Bean first
scanner.scan(packageToScan);
/
/ Finds all BeanDefinitionHolders of @Service whether @ComponentScan scans or not.
Set<BeanDefinitionHolder> beanDefinitionHolders =
findServiceBeanDefinitionHolders(scanner, packageToScan, registry, beanNameGenerator);
if (!CollectionUtils.isEmpty(beanDefinitionHolders)) {
for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
// 构建org.fig.spring.ServiceBean对应的BeanDefinition并注册到BeandefinitionRegistry
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 + "]");
}
}
}
}
重点看下注册ServiceBean代码:
private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, BeanDefinitionRegistry registry,
DubboClassPathBeanDefinitionScanner scanner) {
Class<?> beanClass = resolveClass(beanDefinitionHolder);
Annotation service = findServiceAnnotation(beanClass);
/**
* The {@link AnnotationAttributes} of @Service annotation
*/
AnnotationAttributes serviceAnnotationAttributes = getAnnotationAttributes(service, false, false);
    // 服务接⼝类,可通过@DubboService的interfaceClass或者interfaceName指定,默认为注解标注类对应的接⼝class
Class<?> interfaceClass = resolveServiceInterfaceClass(serviceAnnotationAttributes, beanClass);
String annotatedServiceBeanName = BeanName();
// 构建ServiceBean的BeanDefinition⽤于注册
AbstractBeanDefinition serviceBeanDefinition =
buildServiceBeanDefinition(service, serviceAnnotationAttributes, interfaceClass, annotatedServiceBeanName);
// ServiceBean Bean name
String beanName = generateServiceBeanName(serviceAnnotationAttributes, interfaceClass);
if (scanner.checkCandidate(beanName, serviceBeanDefinition)) { // check duplicated candidate bean
// 注册ServiceBean的BeanDefinition
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?");
}
}
}
private AbstractBeanDefinition buildServiceBeanDefinition(Annotation serviceAnnotation,
AnnotationAttributes serviceAnnotationAttributes,
Class<?> interfaceClass,
String annotatedServiceBeanName) {
BeanDefinitionBuilder builder = rootBeanDefinition(ServiceBean.class);
AbstractBeanDefinition beanDefinition = BeanDefinition();
MutablePropertyValues propertyValues = PropertyValues();
String[] ignoreAttributeNames = of("provider", "monitor", "application", "module", "registry", "protocol",
"interface", "interfaceName", "parameters");
propertyValues.addPropertyValues(new AnnotationPropertyValuesAdapter(serviceAnnotation, environment, ignoreAttributeNames));
// References "ref" property to annotated-@Service Bean
// 指向标注@DubboService注解的原始Bean(在服务暴露的时候会⽤到)
addPropertyReference(builder, "ref", annotatedServiceBeanName);
// Set interface
// 指向@DubboService配置的服务接⼝类名,默认为标注类的接⼝类(在服务暴露的时候会⽤到)
builder.addPropertyValue("interface", Name());
// Convert parameters into map
builder.addPropertyValue("parameters", StringArray("parameters")));
// Add methods parameters
List<MethodConfig> methodConfigs = ("methods"));
if (!methodConfigs.isEmpty()) {
builder.addPropertyValue("methods", methodConfigs);
}
/**
* Add {@link org.fig.ProviderConfig} Bean reference
*/
String providerConfigBeanName = String("provider");
if (StringUtils.hasText(providerConfigBeanName)) {
addPropertyReference(builder, "provider", providerConfigBeanName);
}
/**
* Add {@link org.fig.MonitorConfig} Bean reference
*/
String monitorConfigBeanName = String("monitor");
if (StringUtils.hasText(monitorConfigBeanName)) {
addPropertyReference(builder, "monitor", monitorConfigBeanName);springboot原理图解
}
/**
* Add {@link org.fig.ApplicationConfig} Bean reference
*/
String applicationConfigBeanName = String("application");
if (StringUtils.hasText(applicationConfigBeanName)) {
addPropertyReference(builder, "application", applicationConfigBeanName);
}
/
**
* Add {@link org.fig.ModuleConfig} Bean reference
*/
String moduleConfigBeanName = String("module");
if (StringUtils.hasText(moduleConfigBeanName)) {
addPropertyReference(builder, "module", moduleConfigBeanName);
}
/**
* Add {@link org.fig.RegistryConfig} Bean reference
*/
String[] registryConfigBeanNames = StringArray("registry");
List<RuntimeBeanReference> registryRuntimeBeanReferences = toRuntimeBeanReferences(registryConfigBeanNames);
if (!registryRuntimeBeanReferences.isEmpty()) {
builder.addPropertyValue("registries", registryRuntimeBeanReferences);
}
/**
* Add {@link org.fig.ProtocolConfig} Bean reference
*/
String[] protocolConfigBeanNames = StringArray("protocol");
List<RuntimeBeanReference> protocolRuntimeBeanReferences = toRuntimeBeanReferences(protocolConfigBeanNames);
if (!protocolRuntimeBeanReferences.isEmpty()) {
builder.addPropertyValue("protocols", protocolRuntimeBeanReferences);
}
BeanDefinition();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。