Skip to content

Commit 3388573

Browse files
committed
AbstractApplicationEventMulticaster only caches if event type and source type are cache-safe
Issue: SPR-11606
1 parent 2b52395 commit 3388573

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

spring-context/src/main/java/org/springframework/context/event/AbstractApplicationEventMulticaster.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
import java.util.Set;
2424
import java.util.concurrent.ConcurrentHashMap;
2525

26+
import org.springframework.beans.factory.BeanClassLoaderAware;
2627
import org.springframework.beans.factory.BeanFactory;
2728
import org.springframework.beans.factory.BeanFactoryAware;
2829
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
30+
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2931
import org.springframework.context.ApplicationEvent;
3032
import org.springframework.context.ApplicationListener;
3133
import org.springframework.core.OrderComparator;
34+
import org.springframework.util.ClassUtils;
3235
import org.springframework.util.ObjectUtils;
3336

3437
/**
@@ -50,13 +53,16 @@
5053
* @see #getApplicationListeners(ApplicationEvent)
5154
* @see SimpleApplicationEventMulticaster
5255
*/
53-
public abstract class AbstractApplicationEventMulticaster implements ApplicationEventMulticaster, BeanFactoryAware {
56+
public abstract class AbstractApplicationEventMulticaster
57+
implements ApplicationEventMulticaster, BeanClassLoaderAware, BeanFactoryAware {
5458

5559
private final ListenerRetriever defaultRetriever = new ListenerRetriever(false);
5660

5761
private final Map<ListenerCacheKey, ListenerRetriever> retrieverCache =
5862
new ConcurrentHashMap<ListenerCacheKey, ListenerRetriever>(64);
5963

64+
private ClassLoader beanClassLoader;
65+
6066
private BeanFactory beanFactory;
6167

6268

@@ -102,8 +108,16 @@ public void removeAllListeners() {
102108
}
103109

104110
@Override
105-
public final void setBeanFactory(BeanFactory beanFactory) {
111+
public void setBeanClassLoader(ClassLoader classLoader) {
112+
this.beanClassLoader = classLoader;
113+
}
114+
115+
@Override
116+
public void setBeanFactory(BeanFactory beanFactory) {
106117
this.beanFactory = beanFactory;
118+
if (this.beanClassLoader == null && beanFactory instanceof ConfigurableBeanFactory) {
119+
this.beanClassLoader = ((ConfigurableBeanFactory) beanFactory).getBeanClassLoader();
120+
}
107121
}
108122

109123
private BeanFactory getBeanFactory() {
@@ -179,7 +193,11 @@ protected Collection<ApplicationListener<?>> getApplicationListeners(Application
179193
}
180194
}
181195
OrderComparator.sort(allListeners);
182-
this.retrieverCache.put(cacheKey, retriever);
196+
if (this.beanClassLoader == null ||
197+
(ClassUtils.isCacheSafe(eventType, this.beanClassLoader) &&
198+
(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
199+
this.retrieverCache.put(cacheKey, retriever);
200+
}
183201
return allListeners;
184202
}
185203
}

0 commit comments

Comments
 (0)