public abstract class AbstractApplicationEventMulticaster extends java.lang.Object implements ApplicationEventMulticaster, BeanClassLoaderAware, BeanFactoryAware
ApplicationEventMulticaster
interface,
providing the basic listener registration facility.
Doesn't permit multiple instances of the same listener by default, as it keeps listeners in a linked Set. The collection class used to hold ApplicationListener objects can be overridden through the "collectionClass" bean property.
Implementing ApplicationEventMulticaster's actual ApplicationEventMulticaster.multicastEvent(org.springframework.context.ApplicationEvent)
method
is left to subclasses. SimpleApplicationEventMulticaster
simply multicasts
all events to all registered listeners, invoking them in the calling thread.
Alternative implementations could be more sophisticated in those respects.
getApplicationListeners(ApplicationEvent)
,
SimpleApplicationEventMulticaster
Modifier and Type | Class and Description |
---|---|
private static class |
AbstractApplicationEventMulticaster.ListenerCacheKey
Cache key for ListenerRetrievers, based on event type and source type.
|
private class |
AbstractApplicationEventMulticaster.ListenerRetriever
Helper class that encapsulates a specific set of target listeners,
allowing for efficient retrieval of pre-filtered listeners.
|
Modifier and Type | Field and Description |
---|---|
private java.lang.ClassLoader |
beanClassLoader |
private BeanFactory |
beanFactory |
private AbstractApplicationEventMulticaster.ListenerRetriever |
defaultRetriever |
private java.util.Map<AbstractApplicationEventMulticaster.ListenerCacheKey,AbstractApplicationEventMulticaster.ListenerRetriever> |
retrieverCache |
Constructor and Description |
---|
AbstractApplicationEventMulticaster() |
Modifier and Type | Method and Description |
---|---|
void |
addApplicationListener(ApplicationListener listener)
Add a listener to be notified of all events.
|
void |
addApplicationListenerBean(java.lang.String listenerBeanName)
Add a listener bean to be notified of all events.
|
protected java.util.Collection<ApplicationListener> |
getApplicationListeners()
Return a Collection containing all ApplicationListeners.
|
protected java.util.Collection<ApplicationListener> |
getApplicationListeners(ApplicationEvent event)
Return a Collection of ApplicationListeners matching the given
event type.
|
private BeanFactory |
getBeanFactory() |
void |
removeAllListeners()
Remove all listeners registered with this multicaster.
|
void |
removeApplicationListener(ApplicationListener listener)
Remove a listener from the notification list.
|
void |
removeApplicationListenerBean(java.lang.String listenerBeanName)
Remove a listener bean from the notification list.
|
private java.util.Collection<ApplicationListener> |
retrieveApplicationListeners(java.lang.Class<? extends ApplicationEvent> eventType,
java.lang.Class<?> sourceType,
AbstractApplicationEventMulticaster.ListenerRetriever retriever)
Actually retrieve the application listeners for the given event and source type.
|
void |
setBeanClassLoader(java.lang.ClassLoader classLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
|
protected boolean |
supportsEvent(ApplicationListener listener,
java.lang.Class<? extends ApplicationEvent> eventType,
java.lang.Class<?> sourceType)
Determine whether the given listener supports the given event.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
multicastEvent
private final AbstractApplicationEventMulticaster.ListenerRetriever defaultRetriever
private final java.util.Map<AbstractApplicationEventMulticaster.ListenerCacheKey,AbstractApplicationEventMulticaster.ListenerRetriever> retrieverCache
private java.lang.ClassLoader beanClassLoader
private BeanFactory beanFactory
public void addApplicationListener(ApplicationListener listener)
ApplicationEventMulticaster
addApplicationListener
in interface ApplicationEventMulticaster
listener
- the listener to addpublic void addApplicationListenerBean(java.lang.String listenerBeanName)
ApplicationEventMulticaster
addApplicationListenerBean
in interface ApplicationEventMulticaster
listenerBeanName
- the name of the listener bean to addpublic void removeApplicationListener(ApplicationListener listener)
ApplicationEventMulticaster
removeApplicationListener
in interface ApplicationEventMulticaster
listener
- the listener to removepublic void removeApplicationListenerBean(java.lang.String listenerBeanName)
ApplicationEventMulticaster
removeApplicationListenerBean
in interface ApplicationEventMulticaster
listenerBeanName
- the name of the listener bean to addpublic void removeAllListeners()
ApplicationEventMulticaster
After a remove call, the multicaster will perform no action on event notification until new listeners are being registered.
removeAllListeners
in interface ApplicationEventMulticaster
public void setBeanClassLoader(java.lang.ClassLoader classLoader)
BeanClassLoaderAware
class loader
to
a bean instance.
Invoked after the population of normal bean properties but
before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.
setBeanClassLoader
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
private BeanFactory getBeanFactory()
protected java.util.Collection<ApplicationListener> getApplicationListeners()
ApplicationListener
protected java.util.Collection<ApplicationListener> getApplicationListeners(ApplicationEvent event)
event
- the event to be propagated. Allows for excluding
non-matching listeners early, based on cached matching information.ApplicationListener
private java.util.Collection<ApplicationListener> retrieveApplicationListeners(java.lang.Class<? extends ApplicationEvent> eventType, java.lang.Class<?> sourceType, AbstractApplicationEventMulticaster.ListenerRetriever retriever)
eventType
- the application event typesourceType
- the event source typeretriever
- the ListenerRetriever, if supposed to populate one (for caching purposes)protected boolean supportsEvent(ApplicationListener listener, java.lang.Class<? extends ApplicationEvent> eventType, java.lang.Class<?> sourceType)
The default implementation detects the SmartApplicationListener
interface. In case of a standard ApplicationListener
, a
GenericApplicationListenerAdapter
will be used to introspect
the generically declared type of the target listener.
listener
- the target listener to checkeventType
- the event type to check againstsourceType
- the source type to check against