The @EventListener annotation turns any method on a Spring bean into an application event listener. You no longer implement the ApplicationListener interface or register anything by hand: Spring finds the annotated method, matches its parameter type to the published event, and calls it. This keeps the publisher and the listener decoupled.
It was added in Spring Framework 4.2 and works the same way in Spring 6 and Spring Boot 3. Events can be any object, not only subclasses of ApplicationEvent.
Before Spring 4.2 you had to write a class that implemented ApplicationListener<MyEvent> and register it as a bean. The annotation gives you the same result with a plain method, and one class can hold several listeners for different events.
| Approach | Boilerplate | Event type |
|---|---|---|
ApplicationListener<E> | One class per event type | Must be an ApplicationEvent subtype in older code |
@EventListener | One annotated method | Any object, or a framework event |
Define the event as a record, publish it with ApplicationEventPublisher, and listen with an annotated method.
OrderService knows nothing about EmailNotifier. You can add an audit listener or an analytics listener later without touching the publishing code.
At startup Spring scans beans for methods annotated with @EventListener and registers each one as an application listener. When publishEvent is called, Spring invokes every listener whose parameter type is compatible with the event, in the same thread as the publisher by default. That means:
The condition attribute takes a SpEL expression evaluated against the event, so a listener runs only when it matters.
You can also list several event classes with @EventListener({ContextRefreshedEvent.class, ContextClosedEvent.class}), in which case the method must take no parameter or a common supertype.
If a listener returns a non-null value, Spring publishes it as a new event. This is a compact way to chain steps:
Add @Async to run a listener on another thread, after enabling it with @EnableAsync. The publisher no longer waits, but exceptions are not propagated to it, and the listener will not share the publisher's transaction.
Use @Order to control the sequence of synchronous listeners for the same event; lower values run first.
Spring and Spring Boot publish their own events, which you can listen to the same way. ApplicationReadyEvent is a common choice for start-up work, because it fires after the application is ready to serve requests.
A plain @EventListener fires immediately, even when the surrounding transaction later rolls back. If the listener must act only after a successful commit, use @TransactionalEventListener instead. Its default phase is AFTER_COMMIT.
No. It runs synchronously on the publisher's thread. Add @Async and @EnableAsync for asynchronous execution.
Yes. Since Spring 4.2 any object can be published. Spring wraps it in a PayloadApplicationEvent internally.
For synchronous listeners the exception reaches the code that called publishEvent, and later listeners may not run. Handle exceptions inside the listener if the publisher must not fail.
It should not be. Spring needs to invoke it through the bean proxy, so use a public or at least non-private method on a Spring-managed bean.
@EventListener lets any Spring bean method handle events with no interface and no manual registration. Publish events with ApplicationEventPublisher, filter them with condition, order them with @Order, run them off-thread with @Async, and use @TransactionalEventListener when the reaction must wait for a commit.