美文网首页spring 相关
Spring注解--监听事件:ApplicationListen

Spring注解--监听事件:ApplicationListen

作者: aix91 | 来源:发表于2019-01-13 12:35 被阅读0次

1. 基本用法

  • 定义自己的ApplicationListener
@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println(" my application listener" + event);
    }
}
  • 定义自己的事件
public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}
  • 发布事件
public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        context.publishEvent(new MyApplicationEvent(new String("event")) {
        });
    }

2. 源码分析

  • 在容器创建初始化的时候,initApplicationEventMulticaster()方法会初始化ApplicationEventMulticaster(事件分发器), 创建一个SimpleApplicationEventMulticaster类:
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);

  • 在容器创建的最后的finishRefresh()方法中有一个publishEvent(new ContextRefreshedEvent(this))
protected void publishEvent(Object event, ResolvableType eventType) {
        ...
    //将指定的event的publish给所有的listener
    getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);       
      ...
}
  • multicastEvent,回调所有listener的onApplicationEvent方法
@Override
    public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
          //找到容器中所有的listener,执行listener
        for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
                ...//执行listener
                invokeListener(listener, event);
                ...
            }
        }
    }
  • 容器中的监听器
    在容器初始化中, registerListeners()会将容器中的listener添加到ApplicationEventMulticaster中
protected void registerListeners() {
        // Register statically specified listeners first.
        for (ApplicationListener<?> listener : getApplicationListeners()) {
            getApplicationEventMulticaster().addApplicationListener(listener);
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let post-processors apply to them!
        String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
        for (String listenerBeanName : listenerBeanNames) {
            getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
        }
    }

在multicastEvent方法的getApplicationListeners方法中,会从ApplicationEventMulticaster中取出添加进去的listener

listeners = new LinkedHashSet<ApplicationListener<?>>(this.defaultRetriever.applicationListeners);
listenerBeans = new LinkedHashSet<String>(this.defaultRetriever.applicationListenerBeans);

相关文章

网友评论

    本文标题:Spring注解--监听事件:ApplicationListen

    本文链接:https://www.haomeiwen.com/subject/awukdqtx.html