@Target(value={METHOD,ANNOTATION_TYPE})
@Retention(value=RUNTIME)
@Documented
public @interface EventListener
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 classes()
for further details.
Events can be ApplicationEvent
instances as well as arbitrary
objects.
Processing of @EventListener
annotations is performed via the
EventListenerMethodProcessor
which is registered automatically
when using Java config or manually via the <context:annotation-driven/>
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 event.
It is also possible to define the order in which listeners for a certain
event are invoked. To do so, add Spring's common
@Order
annotation
alongside this 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 UndeclaredThrowableException
since
the caller can only handle runtime exceptions.
EventListenerMethodProcessor
Modifier and Type | Optional Element and Description |
---|---|
java.lang.Class<?>[] |
classes
The event classes that this listener handles.
|
java.lang.String |
condition
Spring Expression Language (SpEL) attribute used for making the event
handling conditional.
|
java.lang.Class<?>[] |
value
Alias for
classes() . |
@AliasFor(value="value") public abstract java.lang.Class<?>[] classes
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.