一 注册xml配置解析函数
public void init() {
registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
registerBeanDefinitionParser("annotation", new AnnotationBeanDefinitionParser());
}
二 DubboBeanDefinitionParser
配置解析.png三 AnnotationBeanDefinitionParser
启动注解支持
- package配置指定扫描包,支持逗号分割多个包路径
- 返回service注解处理bean:ServiceAnnotationBeanPostProcessor
- 注册referenct注解处理bean:ReferenceAnnotationBeanPostProcessor
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String packageToScan = element.getAttribute("package");
String[] packagesToScan = trimArrayElements(commaDelimitedListToStringArray(packageToScan));
builder.addConstructorArgValue(packagesToScan);
builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
// Registers ReferenceAnnotationBeanPostProcessor
registerReferenceAnnotationBeanPostProcessor(parserContext.getRegistry());
}
3.1 ServiceAnnotationBeanPostProcessor
- 实现预处理接口
BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry
- 对目标包,扫描Service注解的类
DubboClassPathBeanDefinitionScanner scanner =
new DubboClassPathBeanDefinitionScanner(registry, environment, resourceLoader);
BeanNameGenerator beanNameGenerator = resolveBeanNameGenerator(registry);
scanner.setBeanNameGenerator(beanNameGenerator);
scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
- 获取注解内容,解析接口类
- 注册service
private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, BeanDefinitionRegistry registry,
DubboClassPathBeanDefinitionScanner scanner) {
Class<?> beanClass = resolveClass(beanDefinitionHolder);
Service service = findAnnotation(beanClass, Service.class);
Class<?> interfaceClass = resolveServiceInterfaceClass(beanClass, service);
String annotatedServiceBeanName = beanDefinitionHolder.getBeanName();
AbstractBeanDefinition serviceBeanDefinition =
buildServiceBeanDefinition(service, interfaceClass, annotatedServiceBeanName);
// ServiceBean Bean name
String beanName = generateServiceBeanName(service, interfaceClass, annotatedServiceBeanName);
if (scanner.checkCandidate(beanName, serviceBeanDefinition)) { // check duplicated candidate bean
registry.registerBeanDefinition(beanName, serviceBeanDefinition);
}
}
3.2 ReferenceAnnotationBeanPostProcessor
- 实现接口postProcessPropertyValues,注入reference接口
- 基于注解配置创建ReferenctBean
- 返回bean代理,注入到目标属性
protected Object doGetInjectedBean(Reference reference, Object bean, String beanName, Class<?> injectedType,
InjectionMetadata.InjectedElement injectedElement) throws Exception {
String referencedBeanName = buildReferencedBeanName(reference, injectedType);
ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referencedBeanName, reference, injectedType, getClassLoader());
cacheInjectedReferenceBean(referenceBean, injectedElement);
Object proxy = buildProxy(referencedBeanName, referenceBean, injectedType);
return proxy;
}
网友评论