EventListener注解原理

两种监听方式

在Spring中,如果要进行事件监听有两种方式:一种是实现ApplicationListener接口,另一种就是在方法上加@EventListener注解。

java 复制代码
@Component
public class MyListener implements ApplicationListener<ApplicationEvent> {

    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        System.out.println("I got it!");
    }
}
java 复制代码
@Component
public class MethodListener {

    @EventListener(classes = ApplicationEvent.class)
    public void listen() {
        System.out.println("I got it!");
    }
}

EventListenerMethodProcessor

那么原理是什么呢? 查看EventListener注解源码,阅读一番后,我们注意到了一个关键类:EventListenerMethodProcessor。看一下源码:

java 复制代码
public class EventListenerMethodProcessor
      implements SmartInitializingSingleton, ApplicationContextAware, BeanFactoryPostProcessor {
    //...
}

通过注释我们可以得知,EventListenerMethodProcessor类的目的就是为了把EventListener注解方法注册成ApplicationListener实例。 除去我们常见的ApplicationContextAware,BeanFactoryPostProcessor两位"老友",剩下有点不熟悉的就是SmartInitializingSingleton了。

SmartInitializingSingleton

java 复制代码
/**
 * Callback interface triggered at the end of the singleton pre-instantiation phase during
 * {@link BeanFactory} bootstrap. 
 * ...
/
public interface SmartInitializingSingleton {
   void afterSingletonsInstantiated();
}

SmartInitializingSingleton接口也很简单,就一个方法。通过注释我们可以得知,它的作用时机就是在单例完成pre-instantiation阶段后作为一个回调接口被触发。这个pre-instantiation,如果看过容器刷新(refresh)的小伙伴们,应该对它非常熟悉。

java 复制代码
//AbstractApplicationContext
@Override
public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
    
      prepareRefresh();
      // ...
        try {
           // ...
        
           // Instantiate all remaining (non-lazy-init) singletons.
           // 在这里
           finishBeanFactoryInitialization(beanFactory);

           // Last step: publish corresponding event.
           finishRefresh();
        }catch(BeanException ex){
            //...
        }
}

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
   
   // ...
   // Instantiate all remaining (non-lazy-init) singletons.
   beanFactory.preInstantiateSingletons();
}
java 复制代码
@Override
public void preInstantiateSingletons() throws BeansException {
    // ...
    // Trigger post-initialization callback for all applicable beans...
    for (String beanName : beanNames) {
       Object singletonInstance = getSingleton(beanName);
       if (singletonInstance instanceof SmartInitializingSingleton) {
          final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
          if (System.getSecurityManager() != null) {
             AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                smartSingleton.afterSingletonsInstantiated();
                return null;
             }, getAccessControlContext());
          }
          else {
             smartSingleton.afterSingletonsInstantiated();
          }
       }
    }
}

没错就是在这里调用了afterSingletonsInstantiated方法。我们看看EventListenerMethodProcessor的afterSingletonsInstantiated方法干了什么。

再探EventListenerMethodProcessor

java 复制代码
//EventListenerMethodProcessor
@Override
public void afterSingletonsInstantiated() {
   ConfigurableListableBeanFactory beanFactory = this.beanFactory;
   Assert.state(this.beanFactory != null, "No ConfigurableListableBeanFactory set");
   String[] beanNames = beanFactory.getBeanNamesForType(Object.class);
   for (String beanName : beanNames) {
      if (!ScopedProxyUtils.isScopedTarget(beanName)) {
         // ...
         if (type != null) {
            // ...
            try {
               processBean(beanName, type);
            }
            catch (Throwable ex) {
              // ...
            }
         }
      }
   }
}
private void processBean(final String beanName, final Class<?> targetType) {
   if (!this.nonAnnotatedClasses.contains(targetType) &&
         AnnotationUtils.isCandidateClass(targetType, EventListener.class) &&
         !isSpringContainerClass(targetType)) {

      Map<Method, EventListener> annotatedMethods = null;
      try {
         annotatedMethods = MethodIntrospector.selectMethods(targetType,
               (MethodIntrospector.MetadataLookup<EventListener>) method ->
                     AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class));
      }
      catch (Throwable ex) {
        // ...
      }

      if (CollectionUtils.isEmpty(annotatedMethods)) {
        // ...
      }
      else {
         // Non-empty set of methods
         ConfigurableApplicationContext context = this.applicationContext;
         Assert.state(context != null, "No ApplicationContext set");
         List<EventListenerFactory> factories = this.eventListenerFactories;
         Assert.state(factories != null, "EventListenerFactory List not initialized");
         for (Method method : annotatedMethods.keySet()) {
            for (EventListenerFactory factory : factories) {
               if (factory.supportsMethod(method)) {
                  // 俺在这
                  Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
                  ApplicationListener<?> applicationListener =
                        factory.createApplicationListener(beanName, targetType, methodToUse);
                  if (applicationListener instanceof ApplicationListenerMethodAdapter) {
                     ((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
                  }
                  context.addApplicationListener(applicationListener);
                  break;
               }
            }
         }
        // ...
      }
   }
}

里面内容很多,咱们直接看重点。从第27行到第32行可以看出,主要目的就是对所有的bean进行检查,找到带有EventListener注解的方法。从第50行到第56行可以看出,就是利用找到的这些方法创建监听器,然后加入容器中。这里也非常巧妙地使用了适配器模式,ApplicationListenerMethodAdapter就是做适配的。 我们再来看看这些方法是如何被使用的呢?

java 复制代码
//ApplicationListenerMethodAdapter
@Override
public void onApplicationEvent(ApplicationEvent event) {
   processEvent(event);
}
public void processEvent(ApplicationEvent event) {
   Object[] args = resolveArguments(event);
   if (shouldHandle(event, args)) {
      Object result = doInvoke(args);
      // ...
   }
}
@Nullable
protected Object doInvoke(Object... args) {
   Object bean = getTargetBean();
   // ...
   try {
      return this.method.invoke(bean, args);
   }
   // ...
}

可以看到当监听到事件后就会执行onApplicationEvent方法,然后去容器中找到这些bean,利用反射执行这些方法。

总结

在容器刷新的倒数第二步中会执行bean中所有实现了SmartInitializingSingleton接口的afterSingletonsInstantiated方法。EventListenerMethodProcessor类重写了该方法,获取所有bean中带有EventListener注解的方法,并使用ApplicationListenerMethodAdapter进行适配,当监听到事件时就执行该方法。

《完》

相关推荐
文丑颜不良啊7 小时前
LeetCode3001~3020题目解析
java·leetcode
山楂树の8 小时前
Java 字符串高效处理:StringBuilder、StringBuffer、StringJoiner
java·开发语言
.痞老板.9 小时前
java ee--文件操作和IO
java·java-ee
简 洁 冬冬9 小时前
分页「向上取整」公式 `(total + size - 1) / size` 的完整推导
java·分页
装不满的克莱因瓶11 小时前
【从零搭建】AI大模型之ReactAgent智能体对接MCP实战
java·人工智能·ai·springai·mcp·springaialibaba
SimonKing12 小时前
AI Assistant被"隐藏"了?别慌,IDEA 2026.3 这波是换了个脑子
java·后端·程序员
Resean022312 小时前
SpringMVC 6+源码分析(九)异常处理
java·spring·springmvc
geats人山人海12 小时前
现代c++4.1
java·开发语言·c++
下一站春风里13 小时前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
挖掘狂人13 小时前
用户连点两次「确认支付」,你的后端凭什么只扣一次钱?
java·后端·面试