AbstractApplicationContext constructor

AbstractApplicationContext()

Creates a new AbstractApplicationContext and scans for lifecycle methods.

The constructor performs initial setup including:

  • Logger initialization for context operations
  • Conversion service setup with default converters
  • Lifecycle method discovery via reflection
  • Internal state initialization

Lifecycle Method Discovery:

Automatically discovers methods annotated with:

  • @OnApplicationStopping
  • @OnApplicationStopped

These methods are invoked during the corresponding lifecycle phases.

Important:

Subclasses should not override this constructor. Instead, override the template methods for custom initialization logic.

The base implementation of a configurable Jetleaf Application Context.

AbstractApplicationContext provides the foundational lifecycle management, environment handling, event bus initialization, and pod factory orchestration that all Jetleaf application contexts build upon.

Key Features:

  • Lifecycle Management: Standardized refresh(), start(), stop(), and close() lifecycle
  • Event System: Integrated event publishing for context lifecycle events
  • Environment Integration: Property source management and profile handling
  • Pod Factory Orchestration: Complete pod lifecycle from registration to destruction
  • Internationalization: Built-in message source support
  • Resource Management: Proper cleanup and resource disposal

Core Lifecycle Events:

  • ContextRefreshedEvent: Published when context is successfully refreshed
  • ContextStartedEvent: Published when context transitions to running state
  • ContextStoppedEvent: Published when context is stopped
  • ContextClosedEvent: Published when context is fully closed

Default Initialization:

  • MessageSource: For internationalization and message resolution
  • ApplicationEventBus: For event publishing and listening
  • LifecycleProcessor: For managing pod lifecycle callbacks
  • Environment: For property resolution and profile management

Usage Pattern:

Application developers typically extend this class to build custom contexts suited for specific runtime environments (web server, CLI, testing, etc.).

class MyCustomApplicationContext extends AbstractApplicationContext {
  @override
  Future<ConfigurableListablePodFactory> doGetFreshPodFactory() async {
    // Provide a custom pod factory implementation
    return MyCustomPodFactory();
  }

  @override
  Future<void> preparePodFactory(ConfigurableListablePodFactory podFactory) async {
    // Register core application pods before refresh
    podFactory.registerSingleton('myService', object: ObjectHolder(MyService()));
    podFactory.registerDefinition('myRepository', MyRepositoryDefinition());
  }

  @override
  Future<void> postProcessPodFactory(ConfigurableListablePodFactory podFactory) async {
    // Apply custom post-processing or decorators
    await super.postProcessPodFactory(podFactory);
    await applyCustomConfiguration(podFactory);
  }
}

void main() async {
  final context = MyCustomApplicationContext();
  
  // Standard lifecycle sequence
  await context.refresh(); // Prepares, initializes, and publishes refresh event
  await context.start();   // Marks context as running and publishes start event
  
  // Application logic here...
  
  await context.stop();    // Stops the context and publishes stop event
  await context.close();   // Closes the context and releases resources
}

Template Methods for Subclasses:

Subclasses must implement these protected template methods:

  • doGetFreshPodFactory(): Provide a fresh pod factory instance
  • preparePodFactory(): Register pods and configure factory before refresh
  • postProcessPodFactory(): Apply custom processing after factory setup

Auto-Startup Behavior:

By default, isAutoStartup() returns true, meaning the context will automatically start after refresh unless overridden by subclasses.

Important Notes:

  • Direct instantiation of AbstractApplicationContext is not recommended
  • Always subclass for specific application needs
  • Ensure proper resource cleanup by implementing doClose()
  • Handle lifecycle exceptions appropriately in subclasses

See also:

Implementation

AbstractApplicationContext() {
  final methods = Runtime.getAllMethods().where((m) => m.getAnnotations().any((a) {
    return a.getType() == OnApplicationStopping || a.getType() == OnApplicationStopped;
  }));

  _lifecycleMethods.addAll(methods.map((m) => Method.declared(m, ProtectionDomain.system())));
  _conversionService = DefaultConversionService();
}