dubbo源码-Refrence注解
前言
前几篇博客registerCommonBeans有个ReferenceAnnotationBeanPostProcessor没有说到,这个处理器就是负责扫描出@Reference注解然后动态代理生成代理对象赋值给该属性的
源码
父类AbstractAnnotationBeanPostProcessor负责扫描出@Reference注解对应的属性。
子类ReferenceAnnotationBeanPostProcessor负责生成代理对象赋值给属性。(服务引入逻辑,本篇博客不涉及)
ReferenceAnnotationBeanPostProcessor
public class ReferenceAnnotationBeanPostProcessor extends AbstractAnnotationBeanPostProcessor implements
ApplicationContextAware {
//将三个注解传给AbstractAnnotationBeanPostProcessor
public ReferenceAnnotationBeanPostProcessor() {
super(DubboReference.class, Reference.class, com.alibaba.dubbo.config.annotation.Reference.class);
}
@Override
protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean, String beanName, Class<?> injectedType,
InjectionMetadata.InjectedElement injectedElement) throws Exception {
//非关键代码省略
//生成代理对象,这里是服务引入的逻辑了,本篇博客不涉及,本篇博客只看父类
return referenceBean.get();
}
}
下面我们看看父类的代码
AbstractAnnotationBeanPostProcessor
public abstract class AbstractAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter{
public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) {
//寻找当前bean有没有dubbo的reference注入点,即当前bean有没有属性添加了注解Reference, 有没有set方法添加了Reference注解
InjectionMetadata metadata = this.findInjectionMetadata(beanName, bean.getClass(), pvs);
//添加了@Reference的属性注入代理对象
metadata.inject(bean, beanName, pvs);
return pvs;
}
}
findInjectionMetadata寻找注入点
private InjectionMetadata findInjectionMetadata(String beanName, Class<?> clazz, PropertyValues pvs) {
AbstractAnnotationBeanPostProcessor.AnnotatedInjectionMetadata metadata = this.buildAnnotatedMetadata(clazz);
return metadata;
}
buildAnnotatedMetadata
private AbstractAnnotationBeanPostProcessor.AnnotatedInjectionMetadata buildAnnotatedMetadata(Class<?> beanClass) {
//@Reference属性
Collection<AbstractAnnotationBeanPostProcessor.AnnotatedFieldElement> fieldElements = this.findFieldAnnotationMetadata(beanClass);
//@Refrence的set方法
Collection<AbstractAnnotationBeanPostProcessor.AnnotatedMethodElement> methodElements = this.findAnnotatedMethodMetadata(beanClass);
//把上面两封装到一起
return new AbstractAnnotationBeanPostProcessor.AnnotatedInjectionMetadata(beanClass, fieldElements, methodElements);
}
findFieldAnnotationMetadata
private List<AbstractAnnotationBeanPostProcessor.AnnotatedFieldElement> findFieldAnnotationMetadata(Class<?> beanClass) {
final List<AbstractAnnotationBeanPostProcessor.AnnotatedFieldElement> elements = new LinkedList();
//我改的伪代码
for(Field field: beanClass.getFileds()){
//@Reference,@Reference(alibaba),@DubboReference
Class[] annoTypes = AbstractAnnotationBeanPostProcessor.this.getAnnotationTypes();
for(Class<? extends Annotation> annotationType:annoTypes ){
//拿到属性添加了@Reference的所有注解信息
AnnotationAttributes attributes = AnnotationUtils.getAnnotationAttributes(field, annotationType, AbstractAnnotationBeanPostProcessor.this.getEnvironment(), true, true, new String[0]);
if (attributes != null) {
if (Modifier.isStatic(field.getModifiers())) {
//不支持static的属性注入
return;
}
//添加到集合
elements.add(AbstractAnnotationBeanPostProcessor.this.new AnnotatedFieldElement(field, attributes));
}
}
}
return elements;
}
注入代理对象metadata.inject
AbstractAnnotationBeanPostProcessor的内部类AnnotatedFieldElement
public class AnnotatedFieldElement extends InjectedElement {
private final Field field;
private final AnnotationAttributes attributes;
private volatile Object bean;
protected AnnotatedFieldElement(Field field, AnnotationAttributes attributes) {
super(field, (PropertyDescriptor)null);
this.field = field;
this.attributes = attributes;
}
//最终属性注入会调用到此处
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
Class<?> injectedType = this.resolveInjectedType(bean, this.field);
//调用子类ReferenceAnnotationBeanPostProcessor的getInjectedObject生成代理对象注入给属性
//服务引用逻辑,很复杂,本篇不涉及
Object injectedObject = AbstractAnnotationBeanPostProcessor.this.getInjectedObject(this.attributes, bean, beanName, injectedType, this);
ReflectionUtils.makeAccessible(this.field);
this.field.set(bean, injectedObject);
}
private Class<?> resolveInjectedType(Object bean, Field field) {
Type genericType = field.getGenericType();
return genericType instanceof Class ? field.getType() : GenericTypeResolver.resolveTypeArgument(AopUtils.getTargetClass(bean), field.getDeclaringClass());
}
}
总结
大体流程:利用spring扩展点postProcessPropertyValues,寻找当前bean有没有dubbo reference注入点,找到后调用注入方法,将服务引入生成的代理对象注入给属性