工作内容

查找并注册监听器。

  1. protected void registerListeners() {
  2. // Register statically specified listeners first.
  3. // 注册来自 prepareRefresh 阶段注册的监听器
  4. for (ApplicationListener<?> listener : getApplicationListeners()) {
  5. getApplicationEventMulticaster().addApplicationListener(listener);
  6. }
  7. // Do not initialize FactoryBeans here: We need to leave all regular beans
  8. // uninitialized to let post-processors apply to them!
  9. // 注册通过接口注册的监听器
  10. String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
  11. for (String listenerBeanName : listenerBeanNames) {
  12. getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
  13. }
  14. // Publish early application events now that we finally have a multicaster...
  15. // 发布消息
  16. Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
  17. this.earlyApplicationEvents = null;
  18. if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
  19. for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
  20. getApplicationEventMulticaster().multicastEvent(earlyEvent);
  21. }
  22. }
  23. }