Skip to content

Commit 19db042

Browse files
committed
Clarify SpEL usage on @EventListener
Issue: SPR-14812
1 parent 7ee0abe commit 19db042

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -87,7 +87,18 @@
8787
/**
8888
* Spring Expression Language (SpEL) attribute used for making the
8989
* event handling conditional.
90-
* <p>Default is "", meaning the event is always handled.
90+
* <p>Default is {@code ""}, meaning the event is always handled.
91+
* <p>The SpEL expression evaluates against a dedicated context that
92+
* provides the following meta-data:
93+
* <ul>
94+
* <li>{@code #root.event}, {@code #root.args} for
95+
* references to the {@link ApplicationEvent} and method arguments
96+
* respectively.</li>
97+
* <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>
101+
* </ul>
91102
*/
92103
String condition() default "";
93104

src/asciidoc/core-beans.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8281,8 +8281,8 @@ event is equal to `foo`:
82818281
[source,java,indent=0]
82828282
[subs="verbatim,quotes"]
82838283
----
8284-
@EventListener(condition = "#event.test == 'foo'")
8285-
public void processBlackListEvent(BlackListEvent event) {
8284+
@EventListener(condition = "#blEvent.test == 'foo'")
8285+
public void processBlackListEvent(BlackListEvent blEvent) {
82868286
// notify appropriate parties via notificationAddress...
82878287
}
82888288
----
@@ -8295,22 +8295,22 @@ available to the context so one can use them for conditional event processing:
82958295
|===
82968296
| Name| Location| Description| Example
82978297

8298-
| event
8298+
| Event
82998299
| root object
83008300
| The actual `ApplicationEvent`
83018301
| `#root.event`
83028302

8303-
| args
8303+
| Arguments array
83048304
| root object
83058305
| The arguments (as array) used for invoking the target
83068306
| `#root.args[0]`
83078307

8308-
| __argument name__
8308+
| __Argument name__
83098309
| evaluation context
83108310
| Name of any of the method arguments. If for some reason the names are not available
83118311
(e.g. no debug information), the argument names are also available under the `#a<#arg>`
83128312
where __#arg__ stands for the argument index (starting from 0).
8313-
| `#iban` or `#a0` (one can also use `#p0` or `#p<#arg>` notation as an alias).
8313+
| `#blEvent` or `#a0` (one can also use `#p0` or `#p<#arg>` notation as an alias).
83148314
|===
83158315

83168316
Note that `#root.event` allows you to access to the underlying event, even if your method

0 commit comments

Comments
 (0)