dubbo起停之服务消费
Posted lccsblog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo起停之服务消费相关的知识,希望对你有一定的参考价值。
ReferenceAnnotationBeanPostProcessor继承了AnnotationInjectedBeanPostProcessors其实现了InstantiationAwareBeanPostProcessorAdapter接口postProcessPropertyValues方法中,扫描带@Reference注解的成员 注入ReferenceBean对象:
@Override public PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException InjectionMetadata metadata = findInjectionMetadata(beanName, bean.getClass(), pvs); try metadata.inject(bean, beanName, pvs); catch (BeanCreationException ex) throw ex; catch (Throwable ex) throw new BeanCreationException(beanName, "Injection of @" + getAnnotationType().getName() + " dependencies is failed", ex); return pvs;
findInjectionMetadata会找出所有@Reference的字段和方法并封装成元数据,metadata.inject方法进去,会调用injectElement的inject方法,进而调用AnnotatedFieldElement的inject方法:
public class AnnotatedFieldElement extends InjectionMetadata.InjectedElement private final Field field; private final A annotation; private volatile Object bean; protected AnnotatedFieldElement(Field field, A annotation) super(field, null); this.field = field; this.annotation = annotation; @Override protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable Class<?> injectedType = field.getType(); Object injectedObject = getInjectedObject(annotation, bean, beanName, injectedType, this); ReflectionUtils.makeAccessible(field); field.set(bean, injectedObject);
protected Object getInjectedObject(A annotation, Object bean, String beanName, Class<?> injectedType, InjectionMetadata.InjectedElement injectedElement) throws Exception String cacheKey = buildInjectedObjectCacheKey(annotation, bean, beanName, injectedType, injectedElement); Object injectedObject = injectedObjectsCache.get(cacheKey); if (injectedObject == null) injectedObject = doGetInjectedBean(annotation, bean, beanName, injectedType, injectedElement); // Customized inject-object if necessary injectedObjectsCache.putIfAbsent(cacheKey, injectedObject); return injectedObject;
其中doGetInjectedBean被ReferenceAnnotationBeanPostProcessor重写:
@Override 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;
可以看到这里构造了一个referenceBean并返回了一个代理,真正注入的其实是这个代理
以上是关于dubbo起停之服务消费的主要内容,如果未能解决你的问题,请参考以下文章