In Spring, @EventListener marks a method as a handler for application events, so beans can react to things that happen elsewhere in the application without depending on the code that triggers them. The publisher calls ApplicationEventPublisher.publishEvent(...), and every matching listener method is invoked.
This article focuses on practical use cases: custom domain events, lifecycle events, conditional and asynchronous handling, and the pitfalls that catch teams in production.
Without events, a service that creates a user must also call the email service, the audit service and the analytics service directly. Every new reaction means editing that service. With events, the user service publishes one UserRegisteredEvent and does not know who listens.
This gives you:
The cost is indirection: the flow is harder to follow from the code, so name events after business facts in the past tense.
Any object can be an event. A Java record is a good fit because events are immutable.
Both listeners receive the same event. @Order makes the sequence predictable for synchronous listeners.
The framework publishes its own events, and @EventListener handles them as well:
| Event | Fires when |
|---|---|
ContextRefreshedEvent | The application context is initialised or refreshed |
ApplicationReadyEvent (Spring Boot) | The application is ready to accept requests |
ContextClosedEvent | The context is closing |
ApplicationFailedEvent (Spring Boot) | Start-up failed |
Some very early Boot events fire before the context exists, so a bean listener cannot receive them. Register those through SpringApplication.addListeners(...) or META-INF/spring.factories-style registration instead.
Three attributes and features cover most needs:
condition accepts a SpEL expression: @EventListener(condition = "#event.email().endsWith('.org')").@Async plus @EnableAsync runs the listener on a task executor, so the publisher does not wait.Remember that asynchronous listeners cannot throw back to the publisher and do not run in its transaction. Configure an AsyncUncaughtExceptionHandler or catch and log inside the method.
A common bug is sending an email from a plain @EventListener inside a transaction that later rolls back, so the user gets a confirmation for data that was never saved. Use @TransactionalEventListener, which runs after commit by default:
If the listener needs to write to the database in that phase, mark it @Transactional(propagation = Propagation.REQUIRES_NEW), because the original transaction is already finished.
Publish an event in a Spring test and assert the effect, or use @RecordApplicationEvents in Spring Framework 5.3.3 and later to capture what was published:
Use it when the publisher should not know its consumers. For a single, mandatory step in the same flow, a direct method call is clearer.
Only with @Order (or by implementing Ordered), and only for synchronous listeners. Asynchronous listeners run concurrently.
No. Spring application events are in-memory and in-process. For durable or cross-service messaging, use a broker such as Kafka or RabbitMQ.
Yes, list the classes in the annotation. The method then takes no parameter or a common supertype.
@EventListener lets Spring beans react to published events with plain methods. Use records as events, add condition, @Order and @Async as needed, switch to @TransactionalEventListener when the reaction depends on a commit, and remember that application events are in-process only.