Search code examples
springevent-handling

Spring @EventListener annotation doesn't work


I want to handle multiple events in one class, here's my example:

@Lazy(false)
@Component
public class EventListenerImpl {

    @EventListener
    public void handleContextRefreshedEvent(ContextRefreshedEvent event) {
        LOGGER.log(event.getSource());
        ...
    }
}

However this method is not being executed when my application starts.

In my applicationContext.xml I have:

<context:annotation-config/>
<context:component-scan base-package="..."/>

which should be enough for @EventListener to work, according to the documentation.

The old way of implementing ApplicationListener<ContextRefreshedEvent> works just fine.

I'm using Spring 4.2.4.RELEASE.


Solution

  • Alright, this remains a total mystery for me. I bet it's some kind of wierd maven/ide caching issue, but anyway this worked for me after several restarts :

    @Lazy(false)
    @Component
    public class EventListenerImpl {
    
        @EventListener
        public void whatever(final ContextRefreshedEvent event) {
            event.getSource();
        }
    }