Support for multiple events per method
In addition to specifying the event type to listen to via a method
parameter, any @EventListener annotated method can now alternatively
define the event type(s) to listen to via the "classes" attributes (that
is aliased to "value").
Something like
@EventListener({FooEvent.class, BarEvent.class})
public void handleFooBar() { .... }
Issue: SPR-13156
This commit is contained in:
@@ -7900,6 +7900,19 @@ As you can see above, the method signature actually _infer_ which even type it l
|
||||
also works for nested generics as long as the actual event resolves the generics parameter you
|
||||
would filter on.
|
||||
|
||||
If your method should listen to several events or if you want to define it with no
|
||||
parameter at all, the event type(s) can also be specified on the annotation itself:
|
||||
|
||||
[source,java,indent=0]
|
||||
[subs="verbatim,quotes"]
|
||||
----
|
||||
@EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})
|
||||
public void handleContextStart() {
|
||||
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
It is also possible to add additional runtime filtering via the `condition` attribute of the
|
||||
annotation that defines a <<expressions,`SpEL` expression>> that should match to actually invoke
|
||||
the method for a particular event.
|
||||
|
||||
Reference in New Issue
Block a user