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:
@@ -113,6 +113,36 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
supportsEventType(true, method, ResolvableType.forClass(PayloadStringTestEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithAnnotationValue() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringAnnotationValue");
|
||||
supportsEventType(true, method, createGenericEventType(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithAnnotationClasses() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringAnnotationClasses");
|
||||
supportsEventType(true, method, createGenericEventType(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithAnnotationValueAndParameter() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringAnnotationValueAndParameter", String.class);
|
||||
supportsEventType(true, method, createGenericEventType(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithSeveralTypes() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringOrInteger");
|
||||
supportsEventType(true, method, createGenericEventType(String.class));
|
||||
supportsEventType(true, method, createGenericEventType(Integer.class));
|
||||
supportsEventType(false, method, createGenericEventType(Double.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithTooManyParameters() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
@@ -131,6 +161,15 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
createTestInstance(method);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithMoreThanOneParameter() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"moreThanOneParameter", String.class, Integer.class);
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
createTestInstance(method);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultOrder() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
@@ -249,6 +288,40 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
verify(this.sampleEvents, never()).handleString(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeListenerWithAnnotationValue() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringAnnotationClasses");
|
||||
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
|
||||
invokeListener(method, event);
|
||||
verify(this.sampleEvents, times(1)).handleStringAnnotationClasses();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeListenerWithAnnotationValueAndParameter() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringAnnotationValueAndParameter", String.class);
|
||||
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
|
||||
invokeListener(method, event);
|
||||
verify(this.sampleEvents, times(1)).handleStringAnnotationValueAndParameter("test");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeListenerWithSeveralTypes() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
"handleStringOrInteger");
|
||||
PayloadApplicationEvent<String> event = new PayloadApplicationEvent<>(this, "test");
|
||||
invokeListener(method, event);
|
||||
verify(this.sampleEvents, times(1)).handleStringOrInteger();
|
||||
PayloadApplicationEvent<Integer> event2 = new PayloadApplicationEvent<>(this, 123);
|
||||
invokeListener(method, event2);
|
||||
verify(this.sampleEvents, times(2)).handleStringOrInteger();
|
||||
PayloadApplicationEvent<Double> event3 = new PayloadApplicationEvent<>(this, 23.2);
|
||||
invokeListener(method, event3);
|
||||
verify(this.sampleEvents, times(2)).handleStringOrInteger();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void beanInstanceRetrievedAtEveryInvocation() {
|
||||
Method method = ReflectionUtils.findMethod(SampleEvents.class,
|
||||
@@ -321,14 +394,32 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
public void handleString(String payload) {
|
||||
}
|
||||
|
||||
@EventListener(String.class)
|
||||
public void handleStringAnnotationValue() {
|
||||
}
|
||||
|
||||
@EventListener(classes = String.class)
|
||||
public void handleStringAnnotationClasses() {
|
||||
}
|
||||
|
||||
@EventListener(String.class)
|
||||
public void handleStringAnnotationValueAndParameter(String payload) {
|
||||
}
|
||||
|
||||
@EventListener({String.class, Integer.class})
|
||||
public void handleStringOrInteger() {
|
||||
}
|
||||
|
||||
@EventListener({String.class, Integer.class})
|
||||
public void handleStringOrIntegerWithParam(String invalid) {
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void handleGenericStringPayload(EntityWrapper<String> event) {
|
||||
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void handleGenericAnyPayload(EntityWrapper<?> event) {
|
||||
|
||||
}
|
||||
|
||||
@EventListener
|
||||
@@ -339,6 +430,10 @@ public class ApplicationListenerMethodAdapterTests extends AbstractApplicationEv
|
||||
public void noParameter() {
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void moreThanOneParameter(String foo, Integer bar) {
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void generateRuntimeException(GenericTestEvent<String> event) {
|
||||
if ("fail".equals(event.getPayload())) {
|
||||
|
||||
Reference in New Issue
Block a user