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

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

@ -16,6 +16,7 @@
package org.springframework.context.event; package org.springframework.context.event;
import java.io.Serializable;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
@ -105,8 +106,9 @@ class AnnotationDrivenEventListenerTests {
this.eventCollector.assertTotalEventsCount(1); this.eventCollector.assertTotalEventsCount(1);
this.eventCollector.clear(); this.eventCollector.clear();
this.context.publishEvent(event); TestEvent otherEvent = new TestEvent(this, Integer.valueOf(1));
this.eventCollector.assertEvent(listener, event); this.context.publishEvent(otherEvent);
this.eventCollector.assertEvent(listener, otherEvent);
this.eventCollector.assertTotalEventsCount(1); this.eventCollector.assertTotalEventsCount(1);
context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l -> context.getBean(ApplicationEventMulticaster.class).removeApplicationListeners(l ->
@ -742,6 +744,11 @@ class AnnotationDrivenEventListenerTests {
public void handleString(String content) { public void handleString(String content) {
collectEvent(content); collectEvent(content);
} }
@EventListener({Boolean.class, Integer.class})
public void handleBooleanOrInteger(Serializable content) {
collectEvent(content);
}
} }
@ -1009,6 +1016,8 @@ class AnnotationDrivenEventListenerTests {
void handleString(String payload); void handleString(String payload);
void handleBooleanOrInteger(Serializable content);
void handleTimestamp(Long timestamp); void handleTimestamp(Long timestamp);
void handleRatio(Double ratio); void handleRatio(Double ratio);
@ -1031,6 +1040,12 @@ class AnnotationDrivenEventListenerTests {
super.handleString(payload); super.handleString(payload);
} }
@EventListener({Boolean.class, Integer.class})
@Override
public void handleBooleanOrInteger(Serializable content) {
super.handleBooleanOrInteger(content);
}
@ConditionalEvent("#root.event.timestamp > #p0") @ConditionalEvent("#root.event.timestamp > #p0")
@Override @Override
public void handleTimestamp(Long timestamp) { 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;
/** /**
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Juergen Hoeller
*/ */
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class TestEvent extends IdentifiableApplicationEvent { public class TestEvent extends IdentifiableApplicationEvent {
public final String msg; public final Object msg;
public TestEvent(Object source, String id, String msg) { public TestEvent(Object source, String id, String msg) {
super(source, id); super(source, id);
@ -34,6 +35,11 @@ public class TestEvent extends IdentifiableApplicationEvent {
this.msg = msg; this.msg = msg;
} }
public TestEvent(Object source, Integer msg) {
super(source);
this.msg = msg;
}
public TestEvent(Object source) { public TestEvent(Object source) {
this(source, "test"); this(source, "test");
} }

Loading…
Cancel
Save