Clarify SpEL usage on `@EventListener`

Issue: SPR-14812
This commit is contained in:
Stephane Nicoll 2016-10-25 17:45:12 +02:00
parent c062835702
commit 196200bcc0
2 changed files with 19 additions and 8 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2015 the original author or authors. * Copyright 2002-2016 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -87,7 +87,18 @@ public @interface EventListener {
/** /**
* Spring Expression Language (SpEL) attribute used for making the * Spring Expression Language (SpEL) attribute used for making the
* event handling conditional. * event handling conditional.
* <p>Default is "", meaning the event is always handled. * <p>Default is {@code ""}, meaning the event is always handled.
* <p>The SpEL expression evaluates against a dedicated context that
* provides the following meta-data:
* <ul>
* <li>{@code #root.event}, {@code #root.args} for
* references to the {@link ApplicationEvent} and method arguments
* respectively.</li>
* <li>Method arguments can be accessed by index. For instance the
* first argument can be accessed via {@code #root.args[0]}, {@code #p0}
* or {@code #a0}. Arguments can also be accessed by name if that
* information is available.</li>
* </ul>
*/ */
String condition() default ""; String condition() default "";

View File

@ -8314,8 +8314,8 @@ event is equal to `foo`:
[source,java,indent=0] [source,java,indent=0]
[subs="verbatim,quotes"] [subs="verbatim,quotes"]
---- ----
@EventListener(condition = "#event.test == 'foo'") @EventListener(condition = "#blEvent.test == 'foo'")
public void processBlackListEvent(BlackListEvent event) { public void processBlackListEvent(BlackListEvent blEvent) {
// notify appropriate parties via notificationAddress... // notify appropriate parties via notificationAddress...
} }
---- ----
@ -8328,22 +8328,22 @@ available to the context so one can use them for conditional event processing:
|=== |===
| Name| Location| Description| Example | Name| Location| Description| Example
| event | Event
| root object | root object
| The actual `ApplicationEvent` | The actual `ApplicationEvent`
| `#root.event` | `#root.event`
| args | Arguments array
| root object | root object
| The arguments (as array) used for invoking the target | The arguments (as array) used for invoking the target
| `#root.args[0]` | `#root.args[0]`
| __argument name__ | __Argument name__
| evaluation context | evaluation context
| Name of any of the method arguments. If for some reason the names are not available | Name of any of the method arguments. If for some reason the names are not available
(e.g. no debug information), the argument names are also available under the `#a<#arg>` (e.g. no debug information), the argument names are also available under the `#a<#arg>`
where __#arg__ stands for the argument index (starting from 0). where __#arg__ stands for the argument index (starting from 0).
| `#iban` or `#a0` (one can also use `#p0` or `#p<#arg>` notation as an alias). | `#blEvent` or `#a0` (one can also use `#p0` or `#p<#arg>` notation as an alias).
|=== |===
Note that `#root.event` allows you to access to the underlying event, even if your method Note that `#root.event` allows you to access to the underlying event, even if your method