这次终于把Spring的监听器讲明白了

听说微信搜索《Java鱼仔》会变更强哦!

本文收录于githubgitee ,里面有我完整的Java系列文章,学习或面试都可以看看哦

(一)Spring中的监听器

监听器可以在使用过程时监听某些事件的发生,然后对这些事件做出响应处理。监听器对应用场景很多,用户的每一个操作都可以被定义为一个事件,通过监听器就能对某些业务场景中的事件进行监听。

Spring中提供了ApplicationListener监听事件,本文会从应用出发讲解Spring的监听器,并逐步深入到源码之中。

(二)监听器的使用

首先自定义一个事件,只需要继承 ApplicationEvent 就可以创建一个自己的事件。

public class MyApplicationEvent extends ApplicationEvent {
    public MyApplicationEvent(Object source) {
        super(source);
    }
}
复制代码

接着定义一个事件监听器,实现当监听到事件后在控制台输出一条消息。

@Component
public class MyListener implements ApplicationListener<MyApplicationEvent> {
    @Override
    public void onApplicationEvent(MyApplicationEvent applicationEvent) {
        System.out.println("事件:"+applicationEvent.toString());
    }
}
复制代码

在配置类中增加包扫描地址。

@Configuration
@ComponentScan(basePackages = "com.javayz.listenerDemo")
public class MainConfig {
}
复制代码

最后在启动容器时发布一条事件,观察结果

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        context.publishEvent(new MyApplicationEvent("我发布了一个事件"));
    }
}
复制代码

通过结果可以发现,除了自定义的事件之外,还有一条Spring自带的事件也被打印出来,修改监听器,只打印自定义的事件:

@Component
public class MyListener implements ApplicationListener<ApplicationEvent> {
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof MyApplicationEvent){
            System.out.println("事件:"+applicationEvent.toString());
        }
    }
}
复制代码

(三)监听器源码分析:

通过 AnnotationConfigApplicationContext 进入Spring源码内部:

   public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
      this();
      register(componentClasses);
      refresh();
   }
复制代码

这三个方法是Spring源码的核心,和监听器相关的内容在refresh方法中。进入refresh方法,和监听器相关的三个方法是initApplicationEventMulticaster()、registerListeners()和finishRefresh();

首先看initApplicationEventMulticaster()

   protected void initApplicationEventMulticaster() {
      ConfigurableListableBeanFactory beanFactory = getBeanFactory();
      if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
         this.applicationEventMulticaster =
               beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
         if (logger.isTraceEnabled()) {
            logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
         }
      }
      else {
         this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
         beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
         if (logger.isTraceEnabled()) {
            logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
                  "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
         }
      }
   }
复制代码

这一段代码很好理解,首先获取ConfigurableListableBeanFactory工厂,然后判断现在Bean工厂里是否有applicationEventMulticaster,如果有的话就获取该多播器,如果没有就新建一个SimpleApplicationEventMulticaster简单多播器并注册到Bean工厂中,这段代码相当于初始化一个多播器。

接下来是registerListeners()方法:

   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);
      }

      // Publish early application events now that we finally have a multicaster...
      Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
      this.earlyApplicationEvents = null;
      if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
         for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
            getApplicationEventMulticaster().multicastEvent(earlyEvent);
         }
      }
   }
复制代码

上面这段注册监听器的代码一共做了三件事情,第一件事情是容器中已有的监听器注册到多播器中;第二件事情是将Bean定义中的事件,也就是我们自己定义的监听器注册到多播器中;第三件事情是发布早期待处理事件,这些早期事件我们定义的监听器是监听不了的,这里发布早期待处理事件时通过multicastEvent()方法进行发布,这个方法会和下面一起讲。

最后看finishRefresh方法,该方法调用的publishEvent方法正式将我们定义的事件发布出去。

protected void finishRefresh() {
   // Clear context-level resource caches (such as ASM metadata from scanning).
   clearResourceCaches();

   // Initialize lifecycle processor for this context.
   initLifecycleProcessor();

   // Propagate refresh to lifecycle processor first.
   getLifecycleProcessor().onRefresh();

   // Publish the final event.
   publishEvent(new ContextRefreshedEvent(this));

   // Participate in LiveBeansView MBean, if active.
   if (!NativeDetector.inNativeImage()) {
      LiveBeansView.registerApplicationContext(this);
   }
}
复制代码

进入publishEvent方法内部,可以看到它的内部同样调用了multicastEvent()方法,说明所有的真正发布动作都是由multicastEvent()完成的。

因此我们有必要来看一下multicastEvent()的发布过程,这里的代码也通俗易懂,判断是否有Executor,如果有的话异步加载invokeListener方法,没有的话同步调用invokeListener方法

public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
   ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
   Executor executor = getTaskExecutor();
   for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
      if (executor != null) {
         executor.execute(() -> invokeListener(listener, event));
      }
      else {
         invokeListener(listener, event);
      }
   }
}
复制代码

在invokeListener()方法中,终于看到了熟悉的onApplicationEvent方法,这个方法就是我们在自定义监听器时重写的方法,也正是在这里监听器调用了我们自己定义的onApplicationEvent(),实现了自定义的一些功能。

(四)总结

通过代码的实践以及源码的解读,监听的原理其实已经很明朗了,Spring的监听器源码可以算是观察者模式的最佳实践,我建议你照着本文的逻辑看一遍监听器源码,肯定会有新的收获。

猜你喜欢

转载自juejin.im/post/7036029249513324574