Mastering Spring Application Events: From Core Listeners to Asynchronous Handling
This article explains how Spring 5.3.23 uses ApplicationEvent and ApplicationListener for event handling, details the standard lifecycle events, shows how to create and publish custom events, and demonstrates annotation‑based, asynchronous, and ordered listeners for robust backend development.
In Spring 5.3.23, event handling in ApplicationContext relies on ApplicationEvent and the ApplicationListener interface; a bean that implements ApplicationListener is notified whenever an ApplicationEvent is published, following the observer pattern.
Up to Spring 4.2 the event infrastructure was significantly improved, adding annotation‑based listeners and the ability to publish arbitrary objects (wrapped as PayloadApplicationEvent when they do not extend ApplicationEvent ).
Standard events provided by Spring include:
ContextRefreshedEvent : published when the ApplicationContext is initialized or refreshed (e.g., via ConfigurableApplicationContext.refresh()).
ContextStartedEvent : published when ConfigurableApplicationContext.start() is called.
ContextStoppedEvent : published when ConfigurableApplicationContext.stop() is called.
ContextClosedEvent : published when ConfigurableApplicationContext.close() or a JVM shutdown hook closes the context.
RequestHandledEvent : a web‑specific event indicating that an HTTP request has been serviced, emitted by DispatcherServlet.
ServletRequestHandledEvent : a subclass of RequestHandledEvent that adds servlet‑specific details.
Lifecycle methods in AbstractApplicationContext invoke finishRefresh(), start(), stop(), and close(), each of which publishes the corresponding event.
Custom events can be created by extending ApplicationEvent, for example:
public class BlockedListEvent extends ApplicationEvent {
private final String address;
private final String content;
public BlockedListEvent(Object source, String address, String content) {
super(source);
this.address = address;
this.content = content;
}
}To publish a custom event, inject ApplicationEventPublisher (often via implementing ApplicationEventPublisherAware) and call publishEvent(new BlockedListEvent(this, address, content)).
Listeners for custom events can either implement ApplicationListener<BlockedListEvent> or use the @EventListener annotation on any bean method:
public class BlockedListNotifier {
private String notificationAddress;
@EventListener
public void processBlockedListEvent(BlockedListEvent event) { /* ... */ }
}The @EventListener annotation can declare multiple event types and be combined with @Async for asynchronous processing. Execution order can be controlled with @Order on the listener method.
Generic events such as EntityCreatedEvent<T> enable type‑safe handling of specific entity creations, provided the generic type is retained in the concrete event class.
Events can be triggered directly via ApplicationEventPublisher.publishEvent() or indirectly via methods annotated with @EventListener, which are processed by EventListenerMethodProcessor.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
