Top Description Inners Fields Constructors Methods
org.springframework.context.event

public abstract Class AbstractApplicationEventMulticaster

extends Object
implements ApplicationEventMulticaster, BeanClassLoaderAware, BeanFactoryAware
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.context.event.ApplicationEventMulticaster
Known Direct Subclasses
org.springframework.context.event.SimpleApplicationEventMulticaster
Imports
java.util.ArrayList, .Collection, .LinkedHashSet, .List, .Map, .Objects, .Set, java.util.concurrent.ConcurrentHashMap, java.util.function.Predicate, org.springframework.aop.framework.AopProxyUtils, org.springframework.beans.factory.BeanClassLoaderAware, .BeanFactory, .BeanFactoryAware, .NoSuchBeanDefinitionException, org.springframework.beans.factory.config.BeanDefinition, .ConfigurableBeanFactory, org.springframework.context.ApplicationEvent, .ApplicationListener, org.springframework.core.ResolvableType, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ObjectUtils

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 multicastEvent method is left to subclasses. SimpleApplicationEventMulticaster simply multicasts all events to all registered listeners, invoking them in the calling thread by default. Alternative implementations could be more sophisticated in those respects.

Authors
Juergen Hoeller, Stephane Nicoll
Since
1.2.3
See Also
getApplicationListeners(ApplicationEvent, ResolvableType), SimpleApplicationEventMulticaster

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
AbstractApplicationEventMulticaster.CachedListenerRetriever

Helper class that encapsulates a specific set of target listeners, allowing for efficient retrieval of pre-filtered listeners.

private class
AbstractApplicationEventMulticaster.DefaultListenerRetriever

Helper class that encapsulates a general set of target listeners.

private static class
AbstractApplicationEventMulticaster.ListenerCacheKey

Cache key for ListenerRetrievers, based on event type and source type.

Field Summary

Modifier and TypeField and Description
private ClassLoader
private ConfigurableBeanFactory
private final AbstractApplicationEventMulticaster.DefaultListenerRetriever
pack-priv final Map<AbstractApplicationEventMulticaster.ListenerCacheKey, AbstractApplicationEventMulticaster.CachedListenerRetriever>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addApplicationListener(ApplicationListener<?>
the listener to add
listener
)

Implements org.springframework.context.event.ApplicationEventMulticaster.addApplicationListener.

Add a listener to be notified of all events.

public void
addApplicationListenerBean(String
the name of the listener bean to add
listenerBeanName
)

Implements org.springframework.context.event.ApplicationEventMulticaster.addApplicationListenerBean.

Add a listener bean to be notified of all events.

protected Collection<ApplicationListener<?>>

Returns:

a Collection of ApplicationListeners
getApplicationListeners
()

Return a Collection containing all ApplicationListeners.

protected Collection<ApplicationListener<?>>

Returns:

a Collection of ApplicationListeners
getApplicationListeners
(ApplicationEvent
the event to be propagated. Allows for excluding non-matching listeners early, based on cached matching information.
event
,
ResolvableType
the event type
eventType
)

Return a Collection of ApplicationListeners matching the given event type.

private ConfigurableBeanFactory
public void
removeAllListeners()

Implements org.springframework.context.event.ApplicationEventMulticaster.removeAllListeners.

Remove all listeners registered with this multicaster.

public void
removeApplicationListener(ApplicationListener<?>
the listener to remove
listener
)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListener.

Remove a listener from the notification list.

public void
removeApplicationListenerBean(String
the name of the listener bean to remove
listenerBeanName
)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListenerBean.

Remove a listener bean from the notification list.

public void
removeApplicationListenerBeans(Predicate<String>
the predicate to identify listener bean names to remove
predicate
)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListenerBeans.

Remove all matching listener beans from the set of registered listener bean names (referring to bean classes which in turn implement the ApplicationListener interface directly).

public void
removeApplicationListeners(Predicate<ApplicationListener<?>>
the predicate to identify listener instances to remove, e.g. checking SmartApplicationListener#getListenerId()
predicate
)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListeners.

Remove all matching listeners from the set of registered ApplicationListener instances (which includes adapter classes such as ApplicationListenerMethodAdapter, e.g. for annotated EventListener methods).

private Collection<ApplicationListener<?>>

Returns:

the pre-filtered list of application listeners for the given event and source type
retrieveApplicationListeners
(ResolvableType
the event type
eventType
,
Class<?>
the event source type
sourceType
,
AbstractApplicationEventMulticaster.CachedListenerRetriever
the ListenerRetriever, if supposed to populate one (for caching purposes)
retriever
)

Actually retrieve the application listeners for the given event and source type.

public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

private boolean

Returns:

whether the given listener should be included in the candidates for the given event type
supportsEvent
(ConfigurableBeanFactory
the BeanFactory that contains the listener beans
beanFactory
,
String
the name of the bean in the BeanFactory
listenerBeanName
,
ResolvableType
the event type to check
eventType
)

Filter a bean-defined listener early through checking its generically declared event type before trying to instantiate it.

protected boolean

Returns:

whether the given listener should be included in the candidates for the given event type
supportsEvent
(Class<?>
the listener's type as determined by the BeanFactory
listenerType
,
ResolvableType
the event type to check
eventType
)

Filter a listener early through checking its generically declared event type before trying to instantiate it.

protected boolean

Returns:

whether the given listener should be included in the candidates for the given event type
supportsEvent
(ApplicationListener<?>
the target listener to check
listener
,
ResolvableType
the event type to check against
eventType
,
Class<?>
the source type to check against
sourceType
)

Determine whether the given listener supports the given event.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanClassLoaderback to summary
private ClassLoader beanClassLoader
Annotations
@Nullable
beanFactoryback to summary
private ConfigurableBeanFactory beanFactory
Annotations
@Nullable
defaultRetrieverback to summary
private final AbstractApplicationEventMulticaster.DefaultListenerRetriever defaultRetriever
retrieverCacheback to summary
pack-priv final Map<AbstractApplicationEventMulticaster.ListenerCacheKey, AbstractApplicationEventMulticaster.CachedListenerRetriever> retrieverCache

Constructor Detail

AbstractApplicationEventMulticasterback to summary
public AbstractApplicationEventMulticaster()

Method Detail

addApplicationListenerback to summary
public void addApplicationListener(ApplicationListener<?> listener)

Implements org.springframework.context.event.ApplicationEventMulticaster.addApplicationListener.

Doc from org.springframework.context.event.ApplicationEventMulticaster.addApplicationListener.

Add a listener to be notified of all events.

Parameters
listener:ApplicationListener<?>

the listener to add

Annotations
@Override
addApplicationListenerBeanback to summary
public void addApplicationListenerBean(String listenerBeanName)

Implements org.springframework.context.event.ApplicationEventMulticaster.addApplicationListenerBean.

Doc from org.springframework.context.event.ApplicationEventMulticaster.addApplicationListenerBean.

Add a listener bean to be notified of all events.

Parameters
listenerBeanName:String

the name of the listener bean to add

Annotations
@Override
getApplicationListenersback to summary
protected Collection<ApplicationListener<?>> getApplicationListeners()

Return a Collection containing all ApplicationListeners.

Returns:Collection<ApplicationListener<?>>

a Collection of ApplicationListeners

See Also
org.springframework.context.ApplicationListener
getApplicationListenersback to summary
protected Collection<ApplicationListener<?>> getApplicationListeners(ApplicationEvent event, ResolvableType eventType)

Return a Collection of ApplicationListeners matching the given event type. Non-matching listeners get excluded early.

Parameters
event:ApplicationEvent

the event to be propagated. Allows for excluding non-matching listeners early, based on cached matching information.

eventType:ResolvableType

the event type

Returns:Collection<ApplicationListener<?>>

a Collection of ApplicationListeners

See Also
org.springframework.context.ApplicationListener
getBeanFactoryback to summary
private ConfigurableBeanFactory getBeanFactory()
removeAllListenersback to summary
public void removeAllListeners()

Implements org.springframework.context.event.ApplicationEventMulticaster.removeAllListeners.

Doc from org.springframework.context.event.ApplicationEventMulticaster.removeAllListeners.

Remove all listeners registered with this multicaster.

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

Annotations
@Override
removeApplicationListenerback to summary
public void removeApplicationListener(ApplicationListener<?> listener)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListener.

Doc from org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListener.

Remove a listener from the notification list.

Parameters
listener:ApplicationListener<?>

the listener to remove

Annotations
@Override
removeApplicationListenerBeanback to summary
public void removeApplicationListenerBean(String listenerBeanName)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListenerBean.

Doc from org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListenerBean.

Remove a listener bean from the notification list.

Parameters
listenerBeanName:String

the name of the listener bean to remove

Annotations
@Override
removeApplicationListenerBeansback to summary
public void removeApplicationListenerBeans(Predicate<String> predicate)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListenerBeans.

Doc from org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListenerBeans.

Remove all matching listener beans from the set of registered listener bean names (referring to bean classes which in turn implement the ApplicationListener interface directly).

Note

This just applies to bean name registrations, not to programmatically registered ApplicationListener instances.

Parameters
predicate:Predicate<String>

the predicate to identify listener bean names to remove

Annotations
@Override
removeApplicationListenersback to summary
public void removeApplicationListeners(Predicate<ApplicationListener<?>> predicate)

Implements org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListeners.

Doc from org.springframework.context.event.ApplicationEventMulticaster.removeApplicationListeners.

Remove all matching listeners from the set of registered ApplicationListener instances (which includes adapter classes such as ApplicationListenerMethodAdapter, e.g. for annotated EventListener methods).

Note

This just applies to instance registrations, not to listeners registered by bean name.

Parameters
predicate:Predicate<ApplicationListener<?>>

the predicate to identify listener instances to remove, e.g. checking SmartApplicationListener#getListenerId()

Annotations
@Override
retrieveApplicationListenersback to summary
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType eventType, Class<?> sourceType, AbstractApplicationEventMulticaster.CachedListenerRetriever retriever)

Actually retrieve the application listeners for the given event and source type.

Parameters
eventType:ResolvableType

the event type

sourceType:Class<?>

the event source type

retriever:AbstractApplicationEventMulticaster.CachedListenerRetriever

the ListenerRetriever, if supposed to populate one (for caching purposes)

Returns:Collection<ApplicationListener<?>>

the pre-filtered list of application listeners for the given event and source type

setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean 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.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

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.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
supportsEventback to summary
private boolean supportsEvent(ConfigurableBeanFactory beanFactory, String listenerBeanName, ResolvableType eventType)

Filter a bean-defined listener early through checking its generically declared event type before trying to instantiate it.

If this method returns true for a given listener as a first pass, the listener instance will get retrieved and fully evaluated through a supportsEvent(ApplicationListener, ResolvableType, Class) call afterwards.

Parameters
beanFactory:ConfigurableBeanFactory

the BeanFactory that contains the listener beans

listenerBeanName:String

the name of the bean in the BeanFactory

eventType:ResolvableType

the event type to check

Returns:boolean

whether the given listener should be included in the candidates for the given event type

See Also
supportsEvent(Class, ResolvableType), supportsEvent(ApplicationListener, ResolvableType, Class)
supportsEventback to summary
protected boolean supportsEvent(Class<?> listenerType, ResolvableType eventType)

Filter a listener early through checking its generically declared event type before trying to instantiate it.

If this method returns true for a given listener as a first pass, the listener instance will get retrieved and fully evaluated through a supportsEvent(ApplicationListener, ResolvableType, Class) call afterwards.

Parameters
listenerType:Class<?>

the listener's type as determined by the BeanFactory

eventType:ResolvableType

the event type to check

Returns:boolean

whether the given listener should be included in the candidates for the given event type

supportsEventback to summary
protected boolean supportsEvent(ApplicationListener<?> listener, ResolvableType eventType, Class<?> sourceType)

Determine whether the given listener supports the given event.

The default implementation detects the SmartApplicationListener and GenericApplicationListener interfaces. In case of a standard ApplicationListener, a GenericApplicationListenerAdapter will be used to introspect the generically declared type of the target listener.

Parameters
listener:ApplicationListener<?>

the target listener to check

eventType:ResolvableType

the event type to check against

sourceType:Class<?>

the source type to check against

Returns:boolean

whether the given listener should be included in the candidates for the given event type

org.springframework.context.event back to summary

private Class AbstractApplicationEventMulticaster.CachedListenerRetriever

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.context.event.AbstractApplicationEventMulticaster.CachedListenerRetriever

Helper class that encapsulates a specific set of target listeners, allowing for efficient retrieval of pre-filtered listeners.

An instance of this helper gets cached per event type and source type.

Field Summary

Modifier and TypeField and Description
public volatile Set<String>
public volatile Set<ApplicationListener<?>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Collection<ApplicationListener<?>>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationListenerBeansback to summary
public volatile Set<String> applicationListenerBeans
Annotations
@Nullable
applicationListenersback to summary
public volatile Set<ApplicationListener<?>> applicationListeners
Annotations
@Nullable

Constructor Detail

CachedListenerRetrieverback to summary
private CachedListenerRetriever()

Method Detail

getApplicationListenersback to summary
public Collection<ApplicationListener<?>> getApplicationListeners()
Annotations
@Nullable
org.springframework.context.event back to summary

private Class AbstractApplicationEventMulticaster.DefaultListenerRetriever

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.context.event.AbstractApplicationEventMulticaster.DefaultListenerRetriever

Helper class that encapsulates a general set of target listeners.

Field Summary

Modifier and TypeField and Description
public final Set<String>
public final Set<ApplicationListener<?>>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Collection<ApplicationListener<?>>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applicationListenerBeansback to summary
public final Set<String> applicationListenerBeans
applicationListenersback to summary
public final Set<ApplicationListener<?>> applicationListeners

Constructor Detail

DefaultListenerRetrieverback to summary
private DefaultListenerRetriever()

Method Detail

getApplicationListenersback to summary
public Collection<ApplicationListener<?>> getApplicationListeners()
org.springframework.context.event back to summary

private final Class AbstractApplicationEventMulticaster.ListenerCacheKey

extends Object
implements Comparable<AbstractApplicationEventMulticaster.ListenerCacheKey>
Class Inheritance
  • java.lang.Object
  • org.springframework.context.event.AbstractApplicationEventMulticaster.ListenerCacheKey
All Implemented Interfaces
java.lang.Comparable

Cache key for ListenerRetrievers, based on event type and source type.

Field Summary

Modifier and TypeField and Description
private final ResolvableType
private final Class<?>

Constructor Summary

AccessConstructor and Description
public
ListenerCacheKey(ResolvableType eventType, Class<?> sourceType)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(AbstractApplicationEventMulticaster.ListenerCacheKey
the object to be compared.
other
)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

eventTypeback to summary
private final ResolvableType eventType
sourceTypeback to summary
private final Class<?> sourceType
Annotations
@Nullable

Constructor Detail

ListenerCacheKeyback to summary
public ListenerCacheKey(ResolvableType eventType, Class<?> sourceType)

Method Detail

compareToback to summary
public int compareTo(AbstractApplicationEventMulticaster.ListenerCacheKey other)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Parameters
other:AbstractApplicationEventMulticaster.ListenerCacheKey

the object to be compared.

Returns:int

a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

Annotations
@Override
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override