ApplicationEventListener<E extends ApplicationEvent> constructor

const ApplicationEventListener<E extends ApplicationEvent>()

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 refreshed
  • ApplicationStoppedEvent - Application context is being stopped
  • ApplicationFailedEvent - Application context failed to refresh
  • CustomDomainEvent - 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
  • ApplicationEventPublisher for event publication
  • EventListener for the non-typed variant
  • SmartApplicationListener for conditional event handling

Implementation

const ApplicationEventListener();