|
23 | 23 | import java.util.Set;
|
24 | 24 | import java.util.concurrent.ConcurrentHashMap;
|
25 | 25 |
|
| 26 | +import org.springframework.beans.factory.BeanClassLoaderAware; |
26 | 27 | import org.springframework.beans.factory.BeanFactory;
|
27 | 28 | import org.springframework.beans.factory.BeanFactoryAware;
|
28 | 29 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
| 30 | +import org.springframework.beans.factory.config.ConfigurableBeanFactory; |
29 | 31 | import org.springframework.context.ApplicationEvent;
|
30 | 32 | import org.springframework.context.ApplicationListener;
|
31 | 33 | import org.springframework.core.OrderComparator;
|
| 34 | +import org.springframework.util.ClassUtils; |
32 | 35 | import org.springframework.util.ObjectUtils;
|
33 | 36 |
|
34 | 37 | /**
|
|
50 | 53 | * @see #getApplicationListeners(ApplicationEvent)
|
51 | 54 | * @see SimpleApplicationEventMulticaster
|
52 | 55 | */
|
53 |
| -public abstract class AbstractApplicationEventMulticaster implements ApplicationEventMulticaster, BeanFactoryAware { |
| 56 | +public abstract class AbstractApplicationEventMulticaster |
| 57 | + implements ApplicationEventMulticaster, BeanClassLoaderAware, BeanFactoryAware { |
54 | 58 |
|
55 | 59 | private final ListenerRetriever defaultRetriever = new ListenerRetriever(false);
|
56 | 60 |
|
57 | 61 | private final Map<ListenerCacheKey, ListenerRetriever> retrieverCache =
|
58 | 62 | new ConcurrentHashMap<ListenerCacheKey, ListenerRetriever>(64);
|
59 | 63 |
|
| 64 | + private ClassLoader beanClassLoader; |
| 65 | + |
60 | 66 | private BeanFactory beanFactory;
|
61 | 67 |
|
62 | 68 |
|
@@ -102,8 +108,16 @@ public void removeAllListeners() {
|
102 | 108 | }
|
103 | 109 |
|
104 | 110 | @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) { |
106 | 117 | this.beanFactory = beanFactory;
|
| 118 | + if (this.beanClassLoader == null && beanFactory instanceof ConfigurableBeanFactory) { |
| 119 | + this.beanClassLoader = ((ConfigurableBeanFactory) beanFactory).getBeanClassLoader(); |
| 120 | + } |
107 | 121 | }
|
108 | 122 |
|
109 | 123 | private BeanFactory getBeanFactory() {
|
@@ -179,7 +193,11 @@ protected Collection<ApplicationListener<?>> getApplicationListeners(Application
|
179 | 193 | }
|
180 | 194 | }
|
181 | 195 | 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 | + } |
183 | 201 | return allListeners;
|
184 | 202 | }
|
185 | 203 | }
|
|
0 commit comments