The @EventListener annotation turns any method of a Spring bean into an application event listener. When another component publishes an event with ApplicationEventPublisher, Spring calls the annotated method, so beans can react to each other without direct dependencies. It has been available since Spring 4.2 and replaces the older need to implement ApplicationListener.
Events are a clean way to decouple "something happened" from "what should happen next", for example sending a welcome email after a user registers.
@TransactionalEventListener@EventListener compared with ApplicationListenerSince Spring 4.2 an event does not have to extend ApplicationEvent; any object can be published. A record works well:
The listener method's parameter type decides which events it receives. A listener for a superclass or interface also receives all subtypes.
Spring itself publishes events that you can listen to with the same annotation:
| Event | When |
|---|---|
ContextRefreshedEvent | The context was initialised or refreshed |
ContextStartedEvent / ContextStoppedEvent | start() or stop() on the context |
ContextClosedEvent | The context is closing |
ApplicationReadyEvent (Spring Boot) | The application is ready to serve requests |
ApplicationFailedEvent (Spring Boot) | Startup failed |
When the annotation lists classes, the method may take no parameter.
Use a SpEL condition to filter events, @Order to control the sequence of listeners, and a return value to publish a follow-up event.
If a listener returns a non-null value, Spring publishes it as a new event. A returned collection or array publishes each element.
Listeners run synchronously on the publisher's thread, inside the publisher's transaction. An exception in a listener propagates to the publisher. To run a listener in the background, add @Async and enable it with @EnableAsync:
Asynchronous listeners cannot return a follow-up event by return value and their exceptions do not reach the publisher, so handle errors inside the method or configure an AsyncUncaughtExceptionHandler.
@TransactionalEventListenerSending an email before the surrounding database transaction commits is a classic bug: the email goes out and the transaction later rolls back. @TransactionalEventListener ties the listener to a transaction phase:
The default phase is AFTER_COMMIT. If no transaction is active when the event is published, the listener is skipped unless you set fallbackExecution = true. Work done in an AFTER_COMMIT listener that needs the database must start a new transaction, for example with @Transactional(propagation = Propagation.REQUIRES_NEW).
@EventListener compared with ApplicationListener| Aspect | @EventListener | ApplicationListener |
|---|---|---|
| Style | Annotation on any method | Interface implementation |
| Event type | Any object | Subclass of ApplicationEvent (generic since 4.2) |
| Several events per class | Yes, one method each | One onApplicationEvent method |
| Conditions | SpEL condition | Manual if |
| Return a new event | Yes | No |
@EventListener methods asynchronous?No. They run synchronously on the publishing thread unless you add @Async with @EnableAsync, or configure an asynchronous ApplicationEventMulticaster.
Listener methods may be package-private or protected in current Spring versions, but the bean must be managed by Spring and the call must pass through the proxy when proxying applies.
For synchronous listeners the exception propagates to the code that published the event and can roll back its transaction. Later listeners for that event do not run.
Yes: list them in the annotation, such as @EventListener({A.class, B.class}), and declare no parameter or a common supertype.
@EventListener registers a method as an application event handler, with any object as the event type, SpEL conditions, ordering and chained events. It is synchronous by default, so use @Async for background work and @TransactionalEventListener when the reaction must wait for a successful commit.