序
本文主要研究一下 dubbo 的 ServiceBeanExportedEvent
ServiceBeanExportedEvent
dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/event/ServiceBeanExportedEvent.java
public class ServiceBeanExportedEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param serviceBean {@link ServiceBean} bean
*/
public ServiceBeanExportedEvent(ServiceBean serviceBean) {super(serviceBean);
}
/**
* Get {@link ServiceBean} instance
*
* @return non-null
*/
public ServiceBean getServiceBean() {return (ServiceBean) super.getSource();}
}
- ServiceBeanExportedEvent 继承了 ApplicationEvent,其 source 为 ServiceBean
ServiceBean
dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/ServiceBean.java
public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean,
ApplicationContextAware, ApplicationListener<ContextRefreshedEvent>, BeanNameAware,
ApplicationEventPublisherAware {
//......
/**
* @since 2.6.5
*/
@Override
public void export() {super.export();
// Publish ServiceBeanExportedEvent
publishExportEvent();}
/**
* @since 2.6.5
*/
private void publishExportEvent() {ServiceBeanExportedEvent exportEvent = new ServiceBeanExportedEvent(this);
applicationEventPublisher.publishEvent(exportEvent);
}
//......
}
- ServiceBean 的 export 方法会 publishExportEvent
ReferenceAnnotationBeanPostProcessor
dubbo-2.7.3/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
public class ReferenceAnnotationBeanPostProcessor extends AnnotationInjectedBeanPostProcessor implements
ApplicationContextAware, ApplicationListener {
//......
@Override
public void onApplicationEvent(ApplicationEvent event) {if (event instanceof ServiceBeanExportedEvent) {onServiceBeanExportEvent((ServiceBeanExportedEvent) event);
} else if (event instanceof ContextRefreshedEvent) {onContextRefreshedEvent((ContextRefreshedEvent) event);
}
}
private void onServiceBeanExportEvent(ServiceBeanExportedEvent event) {ServiceBean serviceBean = event.getServiceBean();
initReferenceBeanInvocationHandler(serviceBean);
}
private void initReferenceBeanInvocationHandler(ServiceBean serviceBean) {String serviceBeanName = serviceBean.getBeanName();
// Remove ServiceBean when it's exported
ReferenceBeanInvocationHandler handler = localReferenceBeanInvocationHandlerCache.remove(serviceBeanName);
// Initialize
if (handler != null) {handler.init();
}
}
//......
}
- ReferenceAnnotationBeanPostProcessor 实现了 ApplicationListener 的 onApplicationEvent 方法,接收到 ServiceBeanExportedEvent 事件时执行 onServiceBeanExportEvent,这里从 localReferenceBeanInvocationHandlerCache 移除,然后执行 ReferenceBeanInvocationHandler 的 init 方法
小结
ServiceBeanExportedEvent 继承了 ApplicationEvent,其 source 为 ServiceBean;ServiceBean 的 export 方法会 publishExportEvent;ReferenceAnnotationBeanPostProcessor 实现了 ApplicationListener 的 onApplicationEvent 方法,接收到 ServiceBeanExportedEvent 事件时执行 onServiceBeanExportEvent,这里从 localReferenceBeanInvocationHandlerCache 移除,然后执行 ReferenceBeanInvocationHandler 的 init 方法
doc
- ServiceBeanExportedEvent