Skip to content

Commit a3fb52e

Browse files
committed
Only use payload if it actually matches declared event type
Closes gh-22426
1 parent 3ec8080 commit a3fb52e

File tree

2 files changed

+82
-8
lines changed

2 files changed

+82
-8
lines changed

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ public void processEvent(ApplicationEvent event) {
186186

187187
/**
188188
* Resolve the method arguments to use for the specified {@link ApplicationEvent}.
189-
* <p>These arguments will be used to invoke the method handled by this instance. Can
190-
* return {@code null} to indicate that no suitable arguments could be resolved and
191-
* therefore the method should not be invoked at all for the specified event.
189+
* <p>These arguments will be used to invoke the method handled by this instance.
190+
* Can return {@code null} to indicate that no suitable arguments could be resolved
191+
* and therefore the method should not be invoked at all for the specified event.
192192
*/
193193
protected Object[] resolveArguments(ApplicationEvent event) {
194194
ResolvableType declaredEventType = getResolvableType(event);
@@ -198,13 +198,15 @@ protected Object[] resolveArguments(ApplicationEvent event) {
198198
if (this.method.getParameterTypes().length == 0) {
199199
return new Object[0];
200200
}
201-
if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) &&
201+
Class<?> eventClass = declaredEventType.getRawClass();
202+
if ((eventClass == null || !ApplicationEvent.class.isAssignableFrom(eventClass)) &&
202203
event instanceof PayloadApplicationEvent) {
203-
return new Object[] {((PayloadApplicationEvent) event).getPayload()};
204-
}
205-
else {
206-
return new Object[] {event};
204+
Object payload = ((PayloadApplicationEvent) event).getPayload();
205+
if (eventClass == null || eventClass.isInstance(payload)) {
206+
return new Object[] {payload};
207+
}
207208
}
209+
return new Object[] {event};
208210
}
209211

210212
protected void handleResult(Object result) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.event;
18+
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.PayloadApplicationEvent;
26+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
27+
import org.springframework.stereotype.Component;
28+
29+
import static org.junit.Assert.*;
30+
31+
/**
32+
* @author Juergen Hoeller
33+
*/
34+
public class PayloadApplicationEventTests {
35+
36+
@Test
37+
public void testEventClassWithInterface() {
38+
ApplicationContext ac = new AnnotationConfigApplicationContext(Listener.class);
39+
MyEventClass event = new MyEventClass<>(this, "xyz");
40+
ac.publishEvent(event);
41+
assertTrue(ac.getBean(Listener.class).events.contains(event));
42+
}
43+
44+
45+
public interface Auditable {
46+
}
47+
48+
49+
public static class MyEventClass<GT> extends PayloadApplicationEvent<GT> implements Auditable {
50+
51+
public MyEventClass(Object source, GT payload) {
52+
super(source, payload);
53+
}
54+
55+
public String toString() {
56+
return "Payload: " + getPayload();
57+
}
58+
}
59+
60+
61+
@Component
62+
public static class Listener {
63+
64+
public final List<Auditable> events = new ArrayList<>();
65+
66+
@EventListener
67+
public void onEvent(Auditable event) {
68+
events.add(event);
69+
}
70+
}
71+
72+
}

0 commit comments

Comments
 (0)