ApplicationEventListener<E extends ApplicationEvent> class
abstract
A listener for a specific type of ApplicationEvent.
Implementations of this interface receive strongly typed application events during the lifecycle of the application, such as context refresh, shutdown, or domain-specific triggers.
This is a typed variant of EventListener and is automatically detected by the Jetleaf event dispatch system when registered in the application context.
Key Features:
- Type Safety: Strongly typed event handling with generic parameter
- Automatic Detection: Automatically registered when defined as a pod
- Lifecycle Integration: Receives events during application lifecycle
- Synchronous Dispatch: Events are typically dispatched synchronously
Event Types:
Common application events include:
ApplicationStartedEvent- Application context has been refreshedApplicationStoppedEvent- Application context is being stoppedApplicationFailedEvent- Application context failed to refreshCustomDomainEvent- Application-specific domain events
Registration:
Listeners are automatically detected and registered when they are:
- Defined as pods in the application context
- Implement the ApplicationEventListener interface
- Specify the exact event type they wish to receive
Example Usage:
class DatabaseReadyListener implements ApplicationListener<DatabaseReadyEvent> {
@override
void onApplicationEvent(DatabaseReadyEvent event) {
print('Database connection established: ${event.connectionId}');
// Perform initialization that requires database access
}
}
Ordering and Priority:
Listeners can implement Ordered or PriorityOrdered to control the order in which they receive events:
class HighPriorityListener implements
ApplicationEventListener<CustomEvent>,
Ordered {
@override
void onApplicationEvent(CustomEvent event) {
// Handle event
}
@override
int getOrder() => Ordered.HIGHEST_PRECEDENCE;
}
Error Handling:
Exceptions thrown in event listeners are caught and logged by the event publisher but do not prevent other listeners from receiving the event.
Thread Safety:
Typically, listeners are invoked synchronously on the same thread that publishes the event. Ensure thread-safe operations if events can be published from multiple threads.
See also:
- ApplicationEvent for the base event class
ApplicationEventPublisherfor event publication- EventListener for the non-typed variant
SmartApplicationListenerfor conditional event handling
- Annotations
-
- @Generic(ApplicationEventListener)
Constructors
- ApplicationEventListener()
-
A listener for a specific type of ApplicationEvent.
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onApplicationEvent(
E event) → Future< void> -
Handle an application event of type
E. -
supportsEventOf(
ApplicationEvent event) → bool - Returns true if the listener supports the given event.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited