ApplicationListener is the Spring interface for reacting to events published inside an ApplicationContext. You implement onApplicationEvent(E event) for a specific event type, register the class as a bean, and Spring calls it whenever a matching event is published. It lets components communicate without direct references to each other.
Spring publishes built-in events such as ContextRefreshedEvent and ContextClosedEvent, and you can publish your own domain events.
The interface is generic and has a single method.
Spring's ApplicationEventMulticaster finds every listener whose generic type matches the published event, including subclasses, and invokes it. By default listeners run synchronously on the publishing thread, in the same transaction context, and an exception in a listener propagates to the publisher.
Common events fired by the container include the following.
| Event | When it fires |
|---|---|
ContextRefreshedEvent | Context initialised or refreshed |
ContextStartedEvent | start() called on the context |
ContextClosedEvent | Context is closing |
ApplicationReadyEvent | Spring Boot: application is ready to serve requests |
ApplicationFailedEvent | Spring Boot: startup failed |
In Spring Boot, ApplicationReadyEvent is a better hook than ContextRefreshedEvent for startup work, because ContextRefreshedEvent can fire more than once in a hierarchy of contexts.
Since Spring 4.2 an event class does not have to extend ApplicationEvent. A plain record works well.
Publish it through ApplicationEventPublisher.
A listener written as the interface needs an event that extends ApplicationEvent.
For plain payload types such as the record above, use the annotation style described next.
The annotation is the modern way and removes the need to implement an interface.
The condition attribute takes a SpEL expression, so you can filter events declaratively. A method can also return a value, and Spring publishes the returned object as a new event.
To avoid blocking the publisher, enable async processing and mark the listener.
When an event must be handled only after a database commit, use @TransactionalEventListener, which by default runs in the AFTER_COMMIT phase. This prevents sending an email for an order that later rolls back.
Use @Order on listener beans or methods to control invocation order for synchronous listeners. With synchronous delivery, an exception thrown by an early listener stops later ones and reaches the publisher. For async listeners, configure an AsyncUncaughtExceptionHandler.
They do the same job. The interface ties a class to one event type, while the annotation works on any method, supports conditions and accepts plain objects as events.
Yes by default. The listener runs on the publisher's thread unless you add @Async or configure a task executor on the multicaster.
No. Application events live inside one ApplicationContext. Use a message broker for communication between services.
Use it for work that needs the application fully started, such as warming caches or registering with an external system.
ApplicationListener connects components through events published in the Spring context. Use it, or preferably @EventListener, to react to container lifecycle events and your own domain events. Remember that delivery is synchronous by default, use @Async for slow work, and choose @TransactionalEventListener when the reaction depends on a successful commit.