Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-14109

Wrong detection of event type on ApplicationListener<> when using lambdas (ClassCast Exception)

    Details

    • Last commented by a User:
      true

      Description

      Hello there,

      i hope this is indeed a Spring Framework and not a Spring Boot bug. If not, please excuse and move it to the right place.

      In my application i have a WebSocketMessageBroker (enabled through @EnableWebSocketMessageBroker) as well as a HttpSessionEventPublisher (enabled via @Bean) and a ApplicationListener<HttpSessionCreatedEvent>.

      If i use an anonymous inner class for the ApplicationListener, everything works fine.

      If i use a lambda like so

      public ApplicationListener<HttpSessionCreatedEvent> httpSessionCreatedEventListener() {
        return (HttpSessionCreatedEvent event) -> {
          LoggerFactory.getLogger(ClasscastApplication.class).info("Session created...");
        };
      }
      

      the generic type of my listener is incorrectly identified and a BrokerAvailabilityEvent is passed to it leading to:

      Caused by: java.lang.ClassCastException: org.springframework.messaging.simp.broker.BrokerAvailabilityEvent cannot be cast to org.springframework.security.web.session.HttpSessionCreatedEvent
      	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:381) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:335) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.publishBrokerAvailableEvent(AbstractBrokerMessageHandler.java:262) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler.startInternal(SimpleBrokerMessageHandler.java:178) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler.start(AbstractBrokerMessageHandler.java:164) ~[spring-messaging-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:173) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
      	... 14 common frames omitted
      

      I guess that might happen with other events, too, but i noticed it in context of messaging.

      I have attached a demo application that reproduces that bug.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                juergen.hoeller Juergen Hoeller
                Reporter:
                michael.simons Michael Simons
                Last updater:
                Juergen Hoeller
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  1 year, 33 weeks, 4 days ago