Skip to content

Commit b7d60b9

Browse files
committed
Improve @eventlistener documentation
1 parent 70cf597 commit b7d60b9

File tree

2 files changed

+57
-26
lines changed

2 files changed

+57
-26
lines changed

spring-context/src/main/java/org/springframework/context/event/EventListener.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,18 +48,39 @@
4848
* return type is either an array or a collection, each element is sent
4949
* as a new individual event.
5050
*
51-
* <p>It is also possible to define the order in which listeners for a
52-
* certain event are to be invoked. To do so, add Spring's common
53-
* {@link org.springframework.core.annotation.Order @Order} annotation
54-
* alongside this event listener annotation.
51+
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
52+
* <em>composed annotations</em>.
5553
*
54+
* <h3>Exception Handling</h3>
5655
* <p>While it is possible for an event listener to declare that it
5756
* throws arbitrary exception types, any checked exceptions thrown
5857
* from an event listener will be wrapped in an
59-
* {@link java.lang.reflect.UndeclaredThrowableException}
58+
* {@link java.lang.reflect.UndeclaredThrowableException UndeclaredThrowableException}
6059
* since the event publisher can only handle runtime exceptions.
6160
*
61+
* <h3>Asynchronous Listeners</h3>
62+
* <p>If you want a particular listener to process events asynchronously, you
63+
* can use Spring's {@link org.springframework.scheduling.annotation.Async @Async}
64+
* support, but be aware of the following limitations when using asynchronous events.
65+
*
66+
* <ul>
67+
* <li>If an asynchronous event listener throws an exception, it is not propagated
68+
* to the caller. See {@link org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
69+
* AsyncUncaughtExceptionHandler} for more details.</li>
70+
* <li>Asynchronous event listener methods cannot publish a subsequent event by returning a
71+
* value. If you need to publish another event as the result of the processing, inject an
72+
* {@link org.springframework.context.ApplicationEventPublisher ApplicationEventPublisher}
73+
* to publish the event manually.</li>
74+
* </ul>
75+
*
76+
* <h3>Ordering Listeners</h3>
77+
* <p>It is also possible to define the order in which listeners for a
78+
* certain event are to be invoked. To do so, add Spring's common
79+
* {@link org.springframework.core.annotation.Order @Order} annotation
80+
* alongside this event listener annotation.
81+
*
6282
* @author Stephane Nicoll
83+
* @author Sam Brannen
6384
* @since 4.2
6485
* @see EventListenerMethodProcessor
6586
*/
@@ -91,13 +112,15 @@
91112
* <p>The SpEL expression evaluates against a dedicated context that
92113
* provides the following meta-data:
93114
* <ul>
94-
* <li>{@code #root.event}, {@code #root.args} for
95-
* references to the {@link ApplicationEvent} and method arguments
96-
* respectively.</li>
115+
* <li>{@code #root.event} or {@code event} for references to the
116+
* {@link ApplicationEvent}</li>
117+
* <li>{@code #root.args} or {@code args} for references to the method
118+
* arguments array</li>
97119
* <li>Method arguments can be accessed by index. For instance the
98-
* first argument can be accessed via {@code #root.args[0]}, {@code #p0}
99-
* or {@code #a0}. Arguments can also be accessed by name if that
100-
* information is available.</li>
120+
* first argument can be accessed via {@code #root.args[0]}, {@code args[0]},
121+
* {@code #a0}, or {@code #p0}. Arguments can also be accessed by name (with
122+
* a preceding hash tag) if that information is available in the compiled
123+
* byte code.</li>
101124
* </ul>
102125
*/
103126
String condition() default "";

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8463,6 +8463,10 @@ The following table describes the standard events that Spring provides:
84638463
| A web-specific event telling all beans that an HTTP request has been serviced. This
84648464
event is published after the request is complete. This event is only applicable to
84658465
web applications that use Spring's `DispatcherServlet`.
8466+
8467+
| `ServletRequestHandledEvent`
8468+
| A subclass of `RequestHandledEvent` that adds Servlet-specific context information.
8469+
84668470
|===
84678471

84688472
You can also create and publish your own custom events. The following example shows a
@@ -8552,8 +8556,10 @@ synchronously. This means that the `publishEvent()` method blocks until all list
85528556
finished processing the event. One advantage of this synchronous and single-threaded
85538557
approach is that, when a listener receives an event, it operates inside the transaction
85548558
context of the publisher if a transaction context is available. If another strategy for
8555-
event publication becomes necessary, See the javadoc for Spring's
8556-
{api-spring-framework}/context/event/ApplicationEventMulticaster.html[`ApplicationEventMulticaster`] interface.
8559+
event publication becomes necessary, see the javadoc for Spring's
8560+
{api-spring-framework}/context/event/ApplicationEventMulticaster.html[`ApplicationEventMulticaster`] interface
8561+
and {api-spring-framework}/context/event/SimpleApplicationEventMulticaster.html[`SimpleApplicationEventMulticaster`]
8562+
implementation for configuration options.
85578563

85588564
The following example shows the bean definitions used to register and configure each of
85598565
the classes above:
@@ -8595,7 +8601,7 @@ architectures that build upon the well-known Spring programming model.
85958601
==== Annotation-based Event Listeners
85968602

85978603
As of Spring 4.2, you can register an event listener on any public method of a managed
8598-
bean by using the `EventListener` annotation. The `BlackListNotifier` can be rewritten as
8604+
bean by using the `@EventListener` annotation. The `BlackListNotifier` can be rewritten as
85998605
follows:
86008606

86018607
[source,java,indent=0]
@@ -8661,19 +8667,20 @@ items made available to the context so that you can use them for conditional eve
86618667
| Event
86628668
| root object
86638669
| The actual `ApplicationEvent`.
8664-
| `#root.event`
8670+
| `#root.event` or `event`
86658671

86668672
| Arguments array
86678673
| root object
8668-
| The arguments (as array) used for invoking the target.
8669-
| `#root.args[0]`
8674+
| The arguments (as an object array) used to invoke the method.
8675+
| `#root.args` or `args`; `args[0]` to access the first argument, etc.
86708676

86718677
| __Argument name__
86728678
| evaluation context
86738679
| The name of any of the method arguments. If, for some reason, the names are not available
8674-
(for example, because there is no debug information), the argument names are also available under the `#a<#arg>`
8675-
where `#arg` stands for the argument index (starting from 0).
8676-
| `#blEvent` or `#a0` (you can also use `#p0` or `#p<#arg>` notation as an alias)
8680+
(for example, because there is no debug information in the compiled byte code), individual
8681+
arguments are also available using the `#a<#arg>` syntax where `<#arg>` stands for the
8682+
argument index (starting from 0).
8683+
| `#blEvent` or `#a0` (you can also use `#p0` or `#p<#arg>` parameter notation as an alias)
86778684
|===
86788685

86798686
Note that `#root.event` gives you access to the underlying event, even if your method
@@ -8719,11 +8726,12 @@ following example shows how to do so:
87198726

87208727
Be aware of the following limitations when using asynchronous events:
87218728

8722-
* If the event listener throws an `Exception`, it is not propagated to the caller
8723-
See `AsyncUncaughtExceptionHandler` for more details.
8724-
* Such event listener cannot send replies. If you need to send another event as the
8725-
result of the processing, inject {api-spring-framework}/aop/interceptor/AsyncUncaughtExceptionHandler.html[`ApplicationEventPublisher`] to send the event
8726-
manually.
8729+
* If an asynchronous event listener throws an `Exception`, it is not propagated to the
8730+
caller. See `AsyncUncaughtExceptionHandler` for more details.
8731+
* Asynchronous event listener methods cannot publish a subsequent event by returning a
8732+
value. If you need to publish another event as the result of the processing, inject an
8733+
{api-spring-framework}/aop/interceptor/AsyncUncaughtExceptionHandler.html[`ApplicationEventPublisher`]
8734+
to publish the event manually.
87278735

87288736

87298737
[[context-functionality-events-order]]

0 commit comments

Comments
 (0)