Skip to content

Commit fbad637

Browse files
committed
SimpleApplicationEventMulticaster just swallows event downcast exceptions
Issue: SPR-14846 (cherry picked from commit 13001b9)
1 parent 01e9307 commit fbad637

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
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: 48 additions & 4 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();
@@ -415,7 +459,7 @@ public MyOtherEvent(Object source) {
415459

416460
public static class MyOrderedListener1 implements ApplicationListener<ApplicationEvent>, Ordered {
417461

418-
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>();
462+
public final Set<ApplicationEvent> seenEvents = new HashSet<>();
419463

420464
@Override
421465
public void onApplicationEvent(ApplicationEvent event) {
@@ -459,7 +503,7 @@ public void onApplicationEvent(MyEvent event) {
459503

460504
public static class MyPayloadListener implements ApplicationListener<PayloadApplicationEvent> {
461505

462-
public final Set<Object> seenPayloads = new HashSet<Object>();
506+
public final Set<Object> seenPayloads = new HashSet<>();
463507

464508
@Override
465509
public void onApplicationEvent(PayloadApplicationEvent event) {
@@ -470,7 +514,7 @@ public void onApplicationEvent(PayloadApplicationEvent event) {
470514

471515
public static class MyNonSingletonListener implements ApplicationListener<ApplicationEvent> {
472516

473-
public static final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>();
517+
public static final Set<ApplicationEvent> seenEvents = new HashSet<>();
474518

475519
@Override
476520
public void onApplicationEvent(ApplicationEvent event) {
@@ -482,7 +526,7 @@ public void onApplicationEvent(ApplicationEvent event) {
482526
@Order(5)
483527
public static class MyOrderedListener3 implements ApplicationListener<ApplicationEvent> {
484528

485-
public final Set<ApplicationEvent> seenEvents = new HashSet<ApplicationEvent>();
529+
public final Set<ApplicationEvent> seenEvents = new HashSet<>();
486530

487531
@Override
488532
public void onApplicationEvent(ApplicationEvent event) {

0 commit comments

Comments
 (0)