org.springframework.context.event
Class AbstractApplicationEventMulticaster

java.lang.Object
  extended by org.springframework.context.event.AbstractApplicationEventMulticaster
All Implemented Interfaces:
BeanFactoryAware, ApplicationEventMulticaster
Direct Known Subclasses:
SimpleApplicationEventMulticaster

public abstract class AbstractApplicationEventMulticaster
extends java.lang.Object
implements ApplicationEventMulticaster, BeanFactoryAware

Abstract implementation of the 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.

Since:
1.2.3
Author:
Juergen Hoeller
See Also:
getApplicationListeners(ApplicationEvent), SimpleApplicationEventMulticaster

Nested Class Summary
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.
 
Field Summary
private  BeanFactory beanFactory
           
private  AbstractApplicationEventMulticaster.ListenerRetriever defaultRetriever
           
private  java.util.Map<AbstractApplicationEventMulticaster.ListenerCacheKey,AbstractApplicationEventMulticaster.ListenerRetriever> retrieverCache
           
 
Constructor Summary
AbstractApplicationEventMulticaster()
           
 
Method Summary
 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.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.context.event.ApplicationEventMulticaster
multicastEvent
 

Field Detail

defaultRetriever

private final AbstractApplicationEventMulticaster.ListenerRetriever defaultRetriever

retrieverCache

private final java.util.Map<AbstractApplicationEventMulticaster.ListenerCacheKey,AbstractApplicationEventMulticaster.ListenerRetriever> retrieverCache

beanFactory

private BeanFactory beanFactory
Constructor Detail

AbstractApplicationEventMulticaster

public AbstractApplicationEventMulticaster()
Method Detail

addApplicationListener

public void addApplicationListener(ApplicationListener listener)
Description copied from interface: ApplicationEventMulticaster
Add a listener to be notified of all events.

Specified by:
addApplicationListener in interface ApplicationEventMulticaster
Parameters:
listener - the listener to add

addApplicationListenerBean

public void addApplicationListenerBean(java.lang.String listenerBeanName)
Description copied from interface: ApplicationEventMulticaster
Add a listener bean to be notified of all events.

Specified by:
addApplicationListenerBean in interface ApplicationEventMulticaster
Parameters:
listenerBeanName - the name of the listener bean to add

removeApplicationListener

public void removeApplicationListener(ApplicationListener listener)
Description copied from interface: ApplicationEventMulticaster
Remove a listener from the notification list.

Specified by:
removeApplicationListener in interface ApplicationEventMulticaster
Parameters:
listener - the listener to remove

removeApplicationListenerBean

public void removeApplicationListenerBean(java.lang.String listenerBeanName)
Description copied from interface: ApplicationEventMulticaster
Remove a listener bean from the notification list.

Specified by:
removeApplicationListenerBean in interface ApplicationEventMulticaster
Parameters:
listenerBeanName - the name of the listener bean to add

removeAllListeners

public void removeAllListeners()
Description copied from interface: ApplicationEventMulticaster
Remove all listeners registered with this multicaster.

After a remove call, the multicaster will perform no action on event notification until new listeners are being registered.

Specified by:
removeAllListeners in interface ApplicationEventMulticaster

setBeanFactory

public final void setBeanFactory(BeanFactory beanFactory)
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean.afterPropertiesSet() or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (never null). The bean can immediately call methods on the factory.
See Also:
BeanInitializationException

getBeanFactory

private BeanFactory getBeanFactory()

getApplicationListeners

protected java.util.Collection<ApplicationListener> getApplicationListeners()
Return a Collection containing all ApplicationListeners.

Returns:
a Collection of ApplicationListeners
See Also:
ApplicationListener

getApplicationListeners

protected java.util.Collection<ApplicationListener> getApplicationListeners(ApplicationEvent event)
Return a Collection of ApplicationListeners matching the given event type. Non-matching listeners get excluded early.

Parameters:
event - the event to be propagated. Allows for excluding non-matching listeners early, based on cached matching information.
Returns:
a Collection of ApplicationListeners
See Also:
ApplicationListener

supportsEvent

protected boolean supportsEvent(ApplicationListener listener,
                                java.lang.Class<? extends ApplicationEvent> eventType,
                                java.lang.Class sourceType)
Determine whether the given listener supports the given event.

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.

Parameters:
listener - the target listener to check
eventType - the event type to check against
sourceType - the source type to check against
Returns:
whether the given listener should be included in the candidates for the given event type