diff --git a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java index 04815d6989..95c8886a74 100644 --- a/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java +++ b/spring-context/src/main/java/org/springframework/context/event/SimpleApplicationEventMulticaster.java @@ -166,8 +166,13 @@ public class SimpleApplicationEventMulticaster extends AbstractApplicationEventM listener.onApplicationEvent(event); } catch (ClassCastException ex) { - // Possibly a lambda-defined listener which we could not resolve the generic event type for - LogFactory.getLog(getClass()).debug("Non-matching event type for listener: " + listener, ex); + if (ex.getMessage().startsWith(event.getClass().getName())) { + // Possibly a lambda-defined listener which we could not resolve the generic event type for + LogFactory.getLog(getClass()).debug("Non-matching event type for listener: " + listener, ex); + } + else { + throw ex; + } } } } diff --git a/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java b/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java index bce850dae1..73e9ebd505 100644 --- a/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java +++ b/spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java @@ -379,6 +379,50 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen context.close(); } + @Test + public void anonymousClassAsListener() { + final Set seenEvents = new HashSet<>(); + StaticApplicationContext context = new StaticApplicationContext(); + context.addApplicationListener(new ApplicationListener() { + @Override + public void onApplicationEvent(MyEvent event) { + seenEvents.add(event); + } + }); + context.refresh(); + + MyEvent event1 = new MyEvent(context); + context.publishEvent(event1); + context.publishEvent(new MyOtherEvent(context)); + MyEvent event2 = new MyEvent(context); + context.publishEvent(event2); + assertSame(2, seenEvents.size()); + assertTrue(seenEvents.contains(event1)); + assertTrue(seenEvents.contains(event2)); + + context.close(); + } + + @Test + public void lambdaAsListener() { + final Set seenEvents = new HashSet<>(); + StaticApplicationContext context = new StaticApplicationContext(); + ApplicationListener listener = seenEvents::add; + context.addApplicationListener(listener); + context.refresh(); + + MyEvent event1 = new MyEvent(context); + context.publishEvent(event1); + context.publishEvent(new MyOtherEvent(context)); + MyEvent event2 = new MyEvent(context); + context.publishEvent(event2); + assertSame(2, seenEvents.size()); + assertTrue(seenEvents.contains(event1)); + assertTrue(seenEvents.contains(event2)); + + context.close(); + } + @Test public void beanPostProcessorPublishesEvents() { GenericApplicationContext context = new GenericApplicationContext();