Skip to content

Commit 13001b9

Browse files
committed
SimpleApplicationEventMulticaster just swallows event downcast exceptions
Issue: SPR-14846
1 parent 2874066 commit 13001b9

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,13 @@ protected void invokeListener(ApplicationListener listener, ApplicationEvent eve
166166
listener.onApplicationEvent(event);
167167
}
168168
catch (ClassCastException ex) {
169-
// Possibly a lambda-defined listener which we could not resolve the generic event type for
170-
LogFactory.getLog(getClass()).debug("Non-matching event type for listener: " + listener, ex);
169+
if (ex.getMessage().startsWith(event.getClass().getName())) {
170+
// Possibly a lambda-defined listener which we could not resolve the generic event type for
171+
LogFactory.getLog(getClass()).debug("Non-matching event type for listener: " + listener, ex);
172+
}
173+
else {
174+
throw ex;
175+
}
171176
}
172177
}
173178
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,50 @@ public void innerBeanAsListener() {
379379
context.close();
380380
}
381381

382+
@Test
383+
public void anonymousClassAsListener() {
384+
final Set<MyEvent> seenEvents = new HashSet<>();
385+
StaticApplicationContext context = new StaticApplicationContext();
386+
context.addApplicationListener(new ApplicationListener<MyEvent>() {
387+
@Override
388+
public void onApplicationEvent(MyEvent event) {
389+
seenEvents.add(event);
390+
}
391+
});
392+
context.refresh();
393+
394+
MyEvent event1 = new MyEvent(context);
395+
context.publishEvent(event1);
396+
context.publishEvent(new MyOtherEvent(context));
397+
MyEvent event2 = new MyEvent(context);
398+
context.publishEvent(event2);
399+
assertSame(2, seenEvents.size());
400+
assertTrue(seenEvents.contains(event1));
401+
assertTrue(seenEvents.contains(event2));
402+
403+
context.close();
404+
}
405+
406+
@Test
407+
public void lambdaAsListener() {
408+
final Set<MyEvent> seenEvents = new HashSet<>();
409+
StaticApplicationContext context = new StaticApplicationContext();
410+
ApplicationListener<MyEvent> listener = seenEvents::add;
411+
context.addApplicationListener(listener);
412+
context.refresh();
413+
414+
MyEvent event1 = new MyEvent(context);
415+
context.publishEvent(event1);
416+
context.publishEvent(new MyOtherEvent(context));
417+
MyEvent event2 = new MyEvent(context);
418+
context.publishEvent(event2);
419+
assertSame(2, seenEvents.size());
420+
assertTrue(seenEvents.contains(event1));
421+
assertTrue(seenEvents.contains(event2));
422+
423+
context.close();
424+
}
425+
382426
@Test
383427
public void beanPostProcessorPublishesEvents() {
384428
GenericApplicationContext context = new GenericApplicationContext();

0 commit comments

Comments
 (0)