首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊dubbo的ServiceBeanExportedEvent

聊聊dubbo的ServiceBeanExportedEvent

作者头像
code4it
发布2019-08-29 09:56:13
6450
发布2019-08-29 09:56:13
举报

本文主要研究一下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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ServiceBeanExportedEvent
  • ServiceBean
  • ReferenceAnnotationBeanPostProcessor
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档