Interview questions
Question 187 of 254
FreeHow do you publish and listen to a custom application event in Spring?
Application Events
Define a plain event class (any POJO, since Spring 4.2 events no longer need to extend ApplicationEvent), inject ApplicationEventPublisher and call `publishEvent(event)` to fire it, and annotate any method with `@EventListener` taking that event type as its parameter to react to it — Spring automatically registers the method as a listener and invokes it whenever a matching event is published.
Intermediate5 min
