Browse Source

Clarify event parameter type for multiple mapped classes

Closes gh-35506
pull/35587/head
Juergen Hoeller 3 months ago
parent
commit
e3da26ebbd
  1. 7
      spring-context/src/main/java/org/springframework/context/event/EventListener.java
  2. 19
      spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java
  3. 8
      spring-context/src/test/java/org/springframework/context/event/test/TestEvent.java

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

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

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

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.context.event;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -105,8 +106,9 @@ class AnnotationDrivenEventListenerTests { @@ -105,8 +106,9 @@ class AnnotationDrivenEventListenerTests {
this.eventCollector.assertTotalEventsCount(1);
this.eventCollector.clear();
this.context.publishEvent(event);
this.eventCollector.assertEvent(listener, event);
TestEvent otherEvent = new TestEvent(this, Integer.valueOf(1));
this.context.publishEvent(otherEvent);
this.eventCollector.assertEvent(listener, otherEvent);
this.eventCollector.assertTotalEventsCount(1);
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
@ -742,6 +744,11 @@ class AnnotationDrivenEventListenerTests { @@ -742,6 +744,11 @@ class AnnotationDrivenEventListenerTests {
public void handleString(String content) {
collectEvent(content);
}
@EventListener({Boolean.class, Integer.class})
public void handleBooleanOrInteger(Serializable content) {
collectEvent(content);
}
}
@ -1009,6 +1016,8 @@ class AnnotationDrivenEventListenerTests { @@ -1009,6 +1016,8 @@ class AnnotationDrivenEventListenerTests {
void handleString(String payload);
void handleBooleanOrInteger(Serializable content);
void handleTimestamp(Long timestamp);
void handleRatio(Double ratio);
@ -1031,6 +1040,12 @@ class AnnotationDrivenEventListenerTests { @@ -1031,6 +1040,12 @@ class AnnotationDrivenEventListenerTests {
super.handleString(payload);
}
@EventListener({Boolean.class, Integer.class})
@Override
public void handleBooleanOrInteger(Serializable content) {
super.handleBooleanOrInteger(content);
}
@ConditionalEvent("#root.event.timestamp > #p0")
@Override
public void handleTimestamp(Long timestamp) {

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

@ -18,11 +18,12 @@ package org.springframework.context.event.test; @@ -18,11 +18,12 @@ package org.springframework.context.event.test;
/**
* @author Stephane Nicoll
* @author Juergen Hoeller
*/
@SuppressWarnings("serial")
public class TestEvent extends IdentifiableApplicationEvent {
public final String msg;
public final Object msg;
public TestEvent(Object source, String id, String msg) {
super(source, id);
@ -34,6 +35,11 @@ public class TestEvent extends IdentifiableApplicationEvent { @@ -34,6 +35,11 @@ public class TestEvent extends IdentifiableApplicationEvent {
this.msg = msg;
}
public TestEvent(Object source, Integer msg) {
super(source);
this.msg = msg;
}
public TestEvent(Object source) {
this(source, "test");
}

Loading…
Cancel
Save