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:
Stephane Nicoll
2015-07-08 14:51:07 +02:00
parent ef0eb01f93
commit bf786c3176
7 changed files with 268 additions and 33 deletions

View File

@@ -25,7 +25,7 @@ import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ApplicationListenerMethodAdapter;
import org.springframework.context.event.EventListener;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -80,8 +80,8 @@ class ApplicationListenerMethodTransactionalAdapter extends ApplicationListenerM
}
static TransactionalEventListener findAnnotation(Method method) {
TransactionalEventListener annotation = AnnotationUtils
.findAnnotation(method, TransactionalEventListener.class);
TransactionalEventListener annotation = AnnotatedElementUtils
.findMergedAnnotation(method, TransactionalEventListener.class);
if (annotation == null) {
throw new IllegalStateException("No TransactionalEventListener annotation found on '" + method + "'");
}

View File

@@ -23,6 +23,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.AliasFor;
/**
* An {@link EventListener} that is invoked according to a {@link TransactionPhase}.
@@ -55,6 +56,21 @@ public @interface TransactionalEventListener {
*/
boolean fallbackExecution() default false;
/**
* Alias for {@link #classes()}.
*/
@AliasFor(attribute = "classes")
Class<?>[] value() default {};
/**
* The event classes that this listener handles. When this attribute is specified
* with one value, the method parameter may or may not be specified. When this
* attribute is specified with more than one value, the method must not have a
* parameter.
*/
@AliasFor(attribute = "value")
Class<?>[] classes() default {};
/**
* Spring Expression Language (SpEL) attribute used for making the event
* handling conditional.