Spring annotation @EventListener not working

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

@Lazy(false)
@Component
public class EventListenerImpl {

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

However, this method does not execute when my application starts.

In mine applicationContext.xmlI have:

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

which should be sufficient to work @EventListenerin accordance with the documentation.

The old implementation method ApplicationListener<ContextRefreshedEvent>works fine.

I am using Spring 4.2.4.RELEASE.

+4
source share
1 answer

Well, that remains a complete mystery to me. I'm sure this is some kind of weird maven / ide caching problem, but in any case, it worked for me after a few restarts:

@Lazy(false)
@Component
public class EventListenerImpl {

    @EventListener
    public void whatever(final ContextRefreshedEvent event) {
        event.getSource();
    }
}
+1
source

Source: https://habr.com/ru/post/1628106/


All Articles