From ca0ce7d6316530255fe1e7e63c89427d6f24b510 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 29 Sep 2018 17:15:53 +0200 Subject: [PATCH] AbstractApplicationEventMulticaster pre-sorts singleton listeners Issue: SPR-17307 (cherry picked from commit 9063e66c5d114855e46b910ab33ae2276fe2bdc2) --- .../AbstractApplicationEventMulticaster.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java b/spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java index ddcffb1e2e..5cd9385027 100644 --- a/spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java +++ b/spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java @@ -239,7 +239,12 @@ public abstract class AbstractApplicationEventMulticaster beanFactory.getBean(listenerBeanName, ApplicationListener.class); if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) { if (retriever != null) { - retriever.applicationListenerBeans.add(listenerBeanName); + if (beanFactory.isSingleton(listenerBeanName)) { + retriever.applicationListeners.add(listener); + } + else { + retriever.applicationListenerBeans.add(listenerBeanName); + } } allListeners.add(listener); } @@ -252,6 +257,10 @@ public abstract class AbstractApplicationEventMulticaster } } AnnotationAwareOrderComparator.sort(allListeners); + if (retriever != null && retriever.applicationListenerBeans.isEmpty()) { + retriever.applicationListeners.clear(); + retriever.applicationListeners.addAll(allListeners); + } return allListeners; } @@ -385,7 +394,9 @@ public abstract class AbstractApplicationEventMulticaster } } } - AnnotationAwareOrderComparator.sort(allListeners); + if (!this.preFiltered || !this.applicationListenerBeans.isEmpty()) { + AnnotationAwareOrderComparator.sort(allListeners); + } return allListeners; } }