Skip to content

Commit e3da26e

Browse files
committed
Clarify event parameter type for multiple mapped classes
Closes gh-35506
1 parent e1c008f commit e3da26e

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@
101101

102102
/**
103103
* The event classes that this listener handles.
104-
* <p>If this attribute is specified with a single value, the
105-
* annotated method may optionally accept a single parameter.
106-
* However, if this attribute is specified with multiple values,
107-
* the annotated method must <em>not</em> declare any parameters.
104+
* <p>The annotated method may optionally accept a single parameter
105+
* of the given event class, or of a common base class or interface
106+
* for all given event classes.
108107
*/
109108
@AliasFor("value")
110109
Class<?>[] classes() default {};

spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.event;
1818

19+
import java.io.Serializable;
1920
import java.lang.annotation.ElementType;
2021
import java.lang.annotation.Retention;
2122
import java.lang.annotation.RetentionPolicy;
@@ -105,8 +106,9 @@ void simpleEventJavaConfig() {
105106
this.eventCollector.assertTotalEventsCount(1);
106107

107108
this.eventCollector.clear();
108-
this.context.publishEvent(event);
109-
this.eventCollector.assertEvent(listener, event);
109+
TestEvent otherEvent = new TestEvent(this, Integer.valueOf(1));
110+
this.context.publishEvent(otherEvent);
111+
this.eventCollector.assertEvent(listener, otherEvent);
110112
this.eventCollector.assertTotalEventsCount(1);
111113

112114
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
@@ -742,6 +744,11 @@ public void handle(TestEvent event) {
742744
public void handleString(String content) {
743745
collectEvent(content);
744746
}
747+
748+
@EventListener({Boolean.class, Integer.class})
749+
public void handleBooleanOrInteger(Serializable content) {
750+
collectEvent(content);
751+
}
745752
}
746753

747754

@@ -1009,6 +1016,8 @@ interface ConditionalEventInterface extends Identifiable {
10091016

10101017
void handleString(String payload);
10111018

1019+
void handleBooleanOrInteger(Serializable content);
1020+
10121021
void handleTimestamp(Long timestamp);
10131022

10141023
void handleRatio(Double ratio);
@@ -1031,6 +1040,12 @@ public void handleString(String payload) {
10311040
super.handleString(payload);
10321041
}
10331042

1043+
@EventListener({Boolean.class, Integer.class})
1044+
@Override
1045+
public void handleBooleanOrInteger(Serializable content) {
1046+
super.handleBooleanOrInteger(content);
1047+
}
1048+
10341049
@ConditionalEvent("#root.event.timestamp > #p0")
10351050
@Override
10361051
public void handleTimestamp(Long timestamp) {

spring-context/src/test/java/org/springframework/context/event/test/TestEvent.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818

1919
/**
2020
* @author Stephane Nicoll
21+
* @author Juergen Hoeller
2122
*/
2223
@SuppressWarnings("serial")
2324
public class TestEvent extends IdentifiableApplicationEvent {
2425

25-
public final String msg;
26+
public final Object msg;
2627

2728
public TestEvent(Object source, String id, String msg) {
2829
super(source, id);
@@ -34,6 +35,11 @@ public TestEvent(Object source, String msg) {
3435
this.msg = msg;
3536
}
3637

38+
public TestEvent(Object source, Integer msg) {
39+
super(source);
40+
this.msg = msg;
41+
}
42+
3743
public TestEvent(Object source) {
3844
this(source, "test");
3945
}

0 commit comments

Comments
 (0)