spring-framework / org.springframework.context.event / EventListener

EventListener

@Target([AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.ANNOTATION_CLASS]) class EventListener

Annotation that marks a method as a listener for application events.

If an annotated method supports a single event type, the method may declare a single parameter that reflects the event type to listen to. If an annotated method supports multiple event types, this annotation may refer to one or more supported event types using the classes attribute. See the #classes javadoc for further details.

Events can be ApplicationEvent instances as well as arbitrary objects.

Processing of @EventListener annotations is performed via the internal EventListenerMethodProcessor bean which gets registered automatically when using Java config or manually via the <context:annotation-config/> or <context:component-scan/> element when using XML config.

Annotated methods may have a non-void return type. When they do, the result of the method invocation is sent as a new event. If the return type is either an array or a collection, each element is sent as a new individual event.

It is also possible to define the order in which listeners for a certain event are to be invoked. To do so, add Spring's common org.springframework.core.annotation.Order annotation alongside this event listener annotation.

While it is possible for an event listener to declare that it throws arbitrary exception types, any checked exceptions thrown from an event listener will be wrapped in an java.lang.reflect.UndeclaredThrowableException since the event publisher can only handle runtime exceptions.

Author
Stephane Nicoll

Since
4.2

See Also
EventListenerMethodProcessor

Constructors

<init>

EventListener(vararg value: KClass<*>, classes: Array<KClass<*>>, condition: String)

Annotation that marks a method as a listener for application events.

If an annotated method supports a single event type, the method may declare a single parameter that reflects the event type to listen to. If an annotated method supports multiple event types, this annotation may refer to one or more supported event types using the classes attribute. See the #classes javadoc for further details.

Events can be ApplicationEvent instances as well as arbitrary objects.

Processing of @EventListener annotations is performed via the internal EventListenerMethodProcessor bean which gets registered automatically when using Java config or manually via the <context:annotation-config/> or <context:component-scan/> element when using XML config.

Annotated methods may have a non-void return type. When they do, the result of the method invocation is sent as a new event. If the return type is either an array or a collection, each element is sent as a new individual event.

It is also possible to define the order in which listeners for a certain event are to be invoked. To do so, add Spring's common org.springframework.core.annotation.Order annotation alongside this event listener annotation.

While it is possible for an event listener to declare that it throws arbitrary exception types, any checked exceptions thrown from an event listener will be wrapped in an java.lang.reflect.UndeclaredThrowableException since the event publisher can only handle runtime exceptions.

Properties

classes

val classes: Array<KClass<*>>

The event classes that this listener handles.

If this attribute is specified with a single value, the annotated method may optionally accept a single parameter. However, if this attribute is specified with multiple values, the annotated method must not declare any parameters.

condition

val condition: String

Spring Expression Language (SpEL) attribute used for making the event handling conditional.

Default is "", meaning the event is always handled.

The SpEL expression evaluates against a dedicated context that provides the following meta-data:

  • #root.event, #root.args for references to the ApplicationEvent and method arguments respectively.
  • Method arguments can be accessed by index. For instance the first argument can be accessed via #root.args[0], #p0 or #a0. Arguments can also be accessed by name if that information is available.

value

val value: Array<KClass<*>>

Alias for #classes.