Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Spring application listeners handle events published inside a Spring application. Use ApplicationListener<E> for a typed listener bean, @EventListener for a flexible method-based listener, and @TransactionalEventListener when handling must wait for a transaction phase. Listeners run synchronously in the publishing thread by default; early Spring Boot lifecycle events need registration before the application context exists.

What Spring application listeners do

Spring’s event-publishing mechanism lets application code publish an event and listeners respond to it. Spring Boot uses the same mechanism for lifecycle events during startup and shutdown. A listener is useful for separating a reaction to something that happened—such as an order being created—from the code that performed the original action.

Events are normally delivered within the application context. In a hierarchy of contexts, events from a child context are also published to ancestor-context listeners, so a listener may receive more than one instance of an event type.

Choose a listener style

Approach Registration and event type Best fit
ApplicationListener<E> Implement the interface and register it as a bean. The generic type identifies the event type Spring should deliver. A dedicated, typed listener class.
@EventListener Annotate a method on a Spring bean. The method can accept an ApplicationEvent subtype or an arbitrary object payload. Concise method-based handling, conditional handling, or simple in-process event pipelines.
@TransactionalEventListener Annotate a method to bind handling to a transaction phase. Work that should run in relation to a transaction’s commit or rollback.
Early Spring Boot listener registration Register with SpringApplication.addListeners(...), SpringApplicationBuilder.listeners(...), or the documented spring.factories key. Lifecycle events emitted before the application context is created.

Use ApplicationListener<E> for a typed listener

ApplicationListener<E> is a functional interface with one method, onApplicationEvent(E event). Implement it and make the listener a Spring bean when ordinary context registration is sufficient. The generic event type expresses which event the listener is interested in, and Spring filters delivery accordingly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Component
class OrderListener implements ApplicationListener<OrderCreatedEvent> {
    @Override
    public void onApplicationEvent(OrderCreatedEvent event) {
        // React to this event
    }
}

Use @EventListener for method-based handling

@EventListener marks a method as an application-event listener. Spring’s EventListenerMethodProcessor discovers and processes these methods on beans. A method can listen for an ApplicationEvent subclass or an arbitrary object published as an event.

@Component
class OrderNotifications {
    @EventListener
    public void onOrderCreated(OrderCreatedEvent event) {
        // React to the order
    }
}

Conditions and ordering

Add a SpEL expression with the annotation’s condition attribute to handle an event only when a predicate is satisfied. To establish a defined relative order among listeners, use @Order on annotated listener methods or implement Ordered where appropriate. Ordering controls listener invocation order; it does not make listeners asynchronous.

Publishing another event from a listener

A non-void @EventListener method publishes its return value as a new event. Returning an array or collection publishes each element as an individual event. This supports simple in-process event chains. An asynchronous listener cannot publish a follow-up event by returning a value; inject ApplicationEventPublisher and publish explicitly if an asynchronous handler must trigger another event.

Understand synchronous and asynchronous execution

By default, listener invocation is synchronous and single-threaded: the listener runs in the thread that publishes the event. When a transaction is available, a synchronous listener also runs within the publisher’s transaction context. Consequently, a slow listener delays the publisher, and an exception from synchronous handling can propagate back to the caller.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use @Async when a particular listener should run asynchronously, with the application’s asynchronous execution support configured. Asynchronous work does not hold up the publisher in the same way, but listener exceptions are not propagated to the publishing caller. Return values from asynchronous listeners also cannot publish follow-up events; publish those explicitly through ApplicationEventPublisher.

Run a listener at a transaction phase

Use @TransactionalEventListener when a handler should be tied to the outcome of a transaction, rather than run immediately at event publication. Its default phase is AFTER_COMMIT, suitable for work that should happen only after the transaction creating the event commits successfully.

@Component
class OrderFollowUp {
    @TransactionalEventListener
    public void afterOrderCreated(OrderCreatedEvent event) {
        // Runs after a successful commit by default
    }
}

The available phases are:

  • BEFORE_COMMIT — before the transaction commits.
  • AFTER_COMMIT — after a successful commit; this is the default.
  • AFTER_ROLLBACK — after a rollback.
  • AFTER_COMPLETION — after transaction completion, whether committed or rolled back.

If no transaction is active, a transactional listener does not run by default. Set fallbackExecution=true only if it should also execute when there is no active transaction. Since Spring Framework 6.1, transaction-bound listeners support both thread-bound and reactive transaction managers. Reactive transaction context is carried through Reactor rather than thread-local state.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Register listeners for early Spring Boot startup events

Spring Boot emits lifecycle events such as ApplicationStartingEvent at the beginning of a run and ApplicationFailedEvent when startup fails. Other startup events include ContextRefreshedEvent and WebServerInitializedEvent. A listener registered as a context bean cannot receive an event emitted before the ApplicationContext exists.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For those early events, register the listener with one of the application bootstrap mechanisms:

  • SpringApplication.addListeners(...)
  • SpringApplicationBuilder.listeners(...)
  • The documented ApplicationListener key in META-INF/spring.factories

For events received through a context hierarchy, compare the injected context with the event’s context when the listener must respond only to its own context. This avoids treating an event from a child or parent context as a local one.

Practical selection guide

  • Choose ApplicationListener<E> for a dedicated class with a clear event type.
  • Choose @EventListener for concise bean methods, SpEL conditions, return-value event publication, or annotation-based ordering.
  • Choose @TransactionalEventListener when the reaction depends on transaction completion or outcome.
  • Choose asynchronous execution for lengthy work that should not block the publishing thread, and handle its exceptions and any subsequent event publication explicitly.
  • Choose bootstrap registration when the event occurs before the application context is available.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.