ApplicationContextAware class abstract interface

🫘 Interface for components that need access to the ApplicationContext.

The ApplicationContextAware interface provides components with access to the broader application context, enabling interaction with application-level services, lifecycle management, and event system.

Key Capabilities:

  • Event Publication: Publish application events to the context
  • Resource Access: Access application-scoped resources and services
  • Lifecycle Management: Participate in application startup/shutdown
  • Profile Detection: Check active profiles and environment settings
  • Internationalization: Access message sources and localization

Framework Integration:

  • Called during application context initialization phase
  • Invoked after PodFactoryAware and PodNameAware callbacks
  • The context is fully configured but not yet refreshed when called

Example Usage:

@Component
class EventPublisherService implements ApplicationContextAware {
  late ApplicationContext _applicationContext;

  @override
  void setApplicationContext(ApplicationContext applicationContext) {
    _applicationContext = applicationContext;
  }

  void publishUserEvent(User user, String action) {
    final event = UserEvent(this, user, action);
    _applicationContext.publishEvent(event);
  }

  bool isProduction() {
    return _applicationContext.getEnvironment()
        .acceptsProfiles({'production'});
  }
}

Best Practices:

  • Use for application-level concerns, not business logic
  • Prefer constructor injection for application-scoped dependencies
  • Be cautious about circular dependencies with the context
  • Consider using ApplicationEventPublisherAware for event-specific needs

See also:

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
setApplicationContext(ApplicationContext applicationContext) β†’ void
Sets the ApplicationContext that this component runs in.
toString() β†’ String
A string representation of this object.
inherited

Operators

operator ==(Object other) β†’ bool
The equality operator.
inherited