AbstractApplicationContext class abstract
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()
, andclose()
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 refreshedContextStartedEvent
: Published when context transitions to running stateContextStoppedEvent
: Published when context is stoppedContextClosedEvent
: 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 instancepreparePodFactory()
: Register pods and configure factory before refreshpostProcessPodFactory()
: 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:
- ConfigurableApplicationContext for the configuration interface
- GenericApplicationContext for a ready-to-use implementation
- AnnotationConfigApplicationContext for annotation-based configuration
- Inheritance
-
- Object
- SmartLifecycle
- ConfigurableApplicationContext
- AbstractApplicationContext
- Implementers
Constructors
- AbstractApplicationContext()
- Creates a new AbstractApplicationContext and scans for lifecycle methods.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- logger → Log
-
The logger associated with this application context.
final
- podFactory ↔ ConfigurableListablePodFactory?
-
The pod factory associated with this application context.
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
addApplicationListener(
ApplicationEventListener< ApplicationEvent> listener) → Future<void> -
Adds an ApplicationEventListener to be notified of all ApplicationEvents.
override
-
addPodAwareProcessor(
PodAwareProcessor processor) → void - Registers a new pod-aware processor.
-
addPodFactoryPostProcessor(
PodFactoryPostProcessor processor) → void -
Adds a PodFactoryPostProcessor to be applied to the pod factory.
override
-
addSingletonCallback(
String name, Class type, Consumer< Object> callback) → void -
Add a callback to be executed when the singleton associated with
name
is initialized. -
cancelRefresh(
PodException exception) → Future< void> -
Cancels the refresh process due to the provided
PodException
. -
clearMetadataCache(
) → void - Clears the internal metadata cache.
-
clearSingletonCache(
) → void - Clears all cached singleton pods.
-
close(
) → Future< void> - Closes this resource, relinquishing any underlying resources.
-
completeRefresh(
) → Future< void> -
Completes the refresh process for the given
ConfigurableListablePodFactory
. -
containsDefinition(
String name) → bool -
Returns
true
if this registry contains a pod with the givenname
. -
containsLocalPod(
String podName) → Future< bool> - Checks if a pod with the given name is registered locally in this factory.
-
containsPod(
String podName) → Future< bool> - Checks if a pod with the given name is registered in the factory.
-
containsSingleton(
String name) → bool -
Returns
true
if this registry contains a pod with the givenname
. -
containsType(
Class type, [bool allowPodProviderInit = false]) → Future< bool> - Checks if the factory contains a pod of the specified type.
-
copyConfigurationFrom(
ConfigurablePodFactory otherFactory) → void - Copies configuration from another factory.
-
destroyPod(
String podName, Object podInstance) → Future< void> - Destroys a pod instance by name and instance.
-
destroyPods(
) → Future< void> -
Destroys all managed pods in the given
ConfigurableListablePodFactory
. -
destroyScopedPod(
String podName) → void - Destroys all pods in the specified scope.
-
destroySingletons(
) → void - Destroys all singleton pods in the factory.
-
doClose(
) → Future< void> - Template method for actual cleanup logic when the context is closed.
-
doGetFreshPodFactory(
) → Future< ConfigurableListablePodFactory> -
Returns a fresh
ConfigurableListablePodFactory
for this context. -
doStart(
) → Future< void> - Template method invoked during start to perform startup logic.
-
doStop(
) → Future< void> - Template method invoked during stop to perform shutdown logic.
-
findAllAnnotationsOnPod<
A> (String podName, Class< A> type) → Future<Set< A> > - Finds all annotations of the specified type on a pod.
-
findAnnotationOnPod<
A> (String podName, Class< A> type) → Future<A?> - Finds a specific annotation on a pod.
-
finishPodFactoryInitialization(
) → Future< void> -
Finalizes the initialization of the
ConfigurableListablePodFactory
. -
finishRefresh(
) → Future< void> - Template method invoked during refresh to complete the refresh process.
-
get<
T> (Class< T> type, [List<ArgumentValue> ? args]) → Future<T> - Retrieves a pod instance by its type with optional arguments.
-
getAliases(
String podName) → List< String> - Retrieves all alias names for the specified pod.
-
getAllowCircularReferences(
) → bool - Retrieves the current circular reference setting.
-
getAllowDefinitionOverriding(
) → bool - Retrieves the current definition overriding setting.
-
getAllowRawInjectionEvenWhenWrapped(
) → bool - Retrieves the current raw injection despite wrapping setting.
-
getApplicationEventBus(
) → ApplicationEventBus -
Returns the ApplicationEventBus currently associated with
this application context.
override
-
getApplicationName(
) → String -
Returns the name of this application.
override
-
getApplicationStartup(
) → ApplicationStartup -
Defines a contract for components that are aware of and can interact with
an
ApplicationStartup
strategy. -
getConversionService(
) → ConversionService -
Retrieves the currently assigned
ConversionService
.override -
getDefinition(
String name) → PodDefinition -
Retrieve the pod registered under
name
. -
getDefinitionNames(
) → List< String> - Returns a list of all pod names currently registered.
-
getDisplayName(
) → String -
Returns the display name of this application context.
inherited
-
getEnvironment(
) → Environment -
🫘 Returns the
Environment
associated with the current context.override -
getId(
) → String -
Returns a unique identifier for this application context.
inherited
-
getIsRefreshed(
) → bool - Returns true if the application context has been refreshed.
-
getMainApplicationClass(
) → Class< Object> -
Returns the main application class.
override
-
getMergedPodDefinition(
String podName) → RootPodDefinition - Retrieves the merged pod definition for the specified pod name.
-
getMessage(
String code, {List< Object> ? args, Locale? locale, String? defaultMessage}) → String -
Retrieve a message for the given code.
override
-
getMessageSource(
) → MessageSource -
Returns the configured MessageSource.
override
-
getNamedObject(
String podName, [List< ArgumentValue> ? args]) → Future<Object> - Retrieves a pod as a generic Object by its name with optional arguments.
-
getNumberOfPodDefinitions(
) → int - Returns the total number of pods registered.
-
getObject(
Class< Object> type, [List<ArgumentValue> ? args]) → Future<Object> - Retrieves a pod as a generic Object by its type with optional arguments.
-
getPackageName(
) → String - Represents an abstraction for identifying the package that an object, resource, or service belongs to.
-
getParent(
) → ApplicationContext? -
Returns the parent context, if any.
override
-
getParentFactory(
) → PodFactory? - Retrieves the parent factory in the hierarchy, if any.
-
getPhase(
) → int -
Interface for objects that may participate in a phased process
such as lifecycle management.
inherited
-
getPod<
T> (String podName, [List< ArgumentValue> ? args, Class<T> ? type]) → Future<T> - Retrieves a pod instance by its name with optional arguments.
-
getPodAwareProcessorCount(
) → int - Gets the number of registered pod-aware processors.
-
getPodAwareProcessors(
) → List< PodAwareProcessor> - Retrieves all registered pod-aware processors.
-
getPodClass(
String podName) → Future< Class> - Retrieves the Class object for the specified pod name.
-
getPodExpressionResolver(
) → PodExpressionResolver? - Retrieves the current expression resolver.
-
getPodFactory(
) → ConfigurableListablePodFactory -
🫘 Returns the underlying
ConfigurableListablePodFactory
.inherited -
getPodFactoryPostProcessors(
) → List< PodFactoryPostProcessor> - The list of PodFactoryPostProcessors to be applied to the pod factory.
-
getPodNames(
Class type, {bool includeNonSingletons = false, bool allowEagerInit = false}) → Future< List< String> > - Retrieves all pod names for pods of the specified type.
-
getPodNamesForAnnotation<
A> (Class< A> type) → Future<List< String> > - Retrieves pod names for pods annotated with the specified annotation type.
-
getPodNamesIterator(
) → Iterator< String> - Returns an iterator over all pod names in the factory.
-
getPodsOf<
T> (Class< T> type, {bool includeNonSingletons = false, bool allowEagerInit = false}) → Future<Map< String, T> > - Retrieves all pods of the specified type as a map of name to instance.
-
getPodsWithAnnotation<
A> (Class< A> type) → Future<Map< String, Object> > - Retrieves pods with the specified annotation as a map of name to instance.
-
getProvider<
T> (Class< T> type, {String? podName, bool allowEagerInit = false}) → Future<ObjectProvider< T> > - Retrieves a provider for a pod, allowing for lazy or eager initialization.
-
getRegisteredScope(
String scopeName) → PodScope? - Retrieves a registered scope by name.
-
getRegisteredScopeNames(
) → List< String> - Retrieves all registered scope names.
-
getSingleton(
String name, {bool allowEarlyReference = true, ObjectFactory< Object> ? factory}) → Future<Object?> -
Retrieve the pod registered under
name
. -
getSingletonCount(
) → int - Returns the total number of pods registered.
-
getSingletonNames(
) → List< String> - Returns a list of all pod names currently registered.
-
getStartTime(
) → DateTime -
Returns the startup time of this context as a DateTime.
override
-
initApplicationEventBus(
) → Future< void> - Initializes the ApplicationEventBus for this context.
-
initLifecycleProcessor(
) → Future< void> -
Initializes the
LifecycleProcessor
for this context. -
initMessageSource(
) → Future< void> - Initializes the MessageSource for this context.
-
invokePodFactoryPostProcessors(
) → Future< void> -
Invokes all registered pod factory post-processors on the given
ConfigurableListablePodFactory
. -
isActive(
) → bool -
Returns whether this application context is currently active.
override
-
isActuallyInCreation(
String podName) → bool - Checks if a pod with the given name is currently being created.
-
isAutoStartup(
) → bool -
An extension of the Lifecycle interface for those objects that require
to be started upon ApplicationContext refresh and/or shutdown in a
particular order.
override
-
isAutowireCandidate(
String podName, DependencyDescriptor descriptor) → bool - Checks if a pod is a candidate for autowiring for the given dependency.
-
isCachePodMetadata(
) → bool - Checks if pod metadata caching is enabled.
-
isClosed(
) → bool -
Returns whether this application context has been closed.
override
-
isNameInUse(
String name) → Future< bool> -
Returns
true
ifname
is currently in use in this registry. -
isPodProvider(
String podName, [RootPodDefinition? rpd]) → Future< bool> - Checks if the specified pod name refers to a pod provider.
-
isPrototype(
String podName) → Future< bool> - Checks if the specified pod is configured as a prototype.
-
isRunning(
) → bool -
Check whether this component is currently running.
override
-
isSingleton(
String podName) → Future< bool> - Checks if the specified pod is configured as a singleton.
-
isTypeMatch(
String name, Class typeToMatch, [bool allowPodProviderInit = false]) → Future< bool> - Checks if the pod with the specified name matches the given type.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
postProcessPodFactory(
) → Future< void> -
Post-processes the
ConfigurableListablePodFactory
for this context. -
preInstantiateSingletons(
) → Future< void> - Pre-instantiates all singleton pods.
-
preparePodFactory(
) → Future< void> -
Prepares the
ConfigurableListablePodFactory
for this context. -
prepareRefresh(
) → Future< void> - Prepares the application context for a refresh.
-
publishEvent(
ApplicationEvent event) → Future< void> -
Publishes the given
event
to all registered ApplicationEventListeners.override -
refresh(
) → Future< void> -
Refreshes this application context:
override
-
register(
PodRegistrar registrar) → void -
Registers a
registrar
that declares pods into this registry.inherited -
registerAlias(
String name, String alias) → void - Registers an alias for an existing name in the registry.
-
registerDefinition(
String name, PodDefinition pod) → Future< void> -
Register a new
pod
under the givenname
. -
registerIgnoredDependency(
Class type) → void - Registers a dependency type that should be ignored during autowiring.
-
registerListeners(
) → Future< void> -
Registers all ApplicationEventListener pods in the
ConfigurableListablePodFactory
. -
registerPod<
T> (Class< T> podClass, {Consumer<Spec< ? customizer, String? name}) → voidT> > -
Registers a pod of type
T
in the registry.inherited -
registerPodAwareProcessors(
) → Future< void> -
Registers all PodAware processors into the given
ConfigurableListablePodFactory
. -
registerResolvableDependency(
Class type, [Object? autowiredValue]) → void - Registers a resolvable dependency for autowiring.
-
registerScope(
String scopeName, PodScope scope) → void - Registers a new scope with the given name.
-
registerSingleton(
String name, Class type, {ObjectHolder< Object> ? object, ObjectFactory<Object> ? factory}) → Future<void> -
Register a new
pod
under the givenname
. -
removeDefinition(
String name) → Future< void> -
Remove the pod associated with
name
. -
removeSingleton(
String name) → void -
Remove the pod associated with
name
. -
resetCommonCaches(
) → Future< void> - Resets common internal caches maintained by the container.
-
resolveDependency(
DependencyDescriptor descriptor, [Set< String> ? candidates]) → Future<Object?> - Resolves a dependency based on the provided descriptor.
-
setAllowCircularReferences(
bool value) → void - Allows or disallows circular references between pods.
-
setAllowDefinitionOverriding(
bool value) → void - Allows or disallows overriding of pod definitions.
-
setAllowRawInjectionEvenWhenWrapped(
bool value) → void - Allows or disallows raw injection despite wrapping.
-
setApplicationEventBus(
ApplicationEventBus applicationEventBus) → void -
Sets the ApplicationEventBus for this application context.
override
-
setApplicationStartup(
ApplicationStartup applicationStartup) → void -
Defines a contract for components that are aware of and can interact with
an
ApplicationStartup
strategy. -
setCachePodMetadata(
bool cachePodMetadata) → void - Enables or disables caching of pod metadata for performance optimization.
-
setConversionService(
ConversionService conversionService) → void -
Sets an assigned
ConversionService
.override -
setEnvironment(
Environment environment) → void -
Sets the
Environment
that this component runs in.override -
setMainApplicationClass(
Class< Object> mainApplicationClass) → void -
Sets the main application class.
override
-
setMessageSource(
MessageSource messageSource) → void -
Sets the MessageSource used for resolving internationalized messages.
override
-
setParent(
ApplicationContext parent) → void -
Sets the parent of this application context.
override
-
setParentFactory(
PodFactory? parentFactory) → void - Sets the parent factory for this hierarchical factory.
-
setPodExpressionResolver(
PodExpressionResolver? valueResolver) → void - Sets the expression resolver for resolving expressions in pod definitions.
-
start(
) → FutureOr< void> -
A common interface defining methods for start/stop lifecycle control.
override
-
stop(
[Runnable? callback]) → FutureOr< void> -
Stop this component, typically in a synchronous fashion.
override
-
supports(
ApplicationType applicationType) → bool -
Returns whether this context supports the given
applicationType
.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- APPLICATION_EVENT_BUS_POD_NAME → String
-
The reserved pod name for the ApplicationEventBus.
final
- APPLICATION_NAME → String
-
The property key used as fallback for application name configuration.
final
- BANNER_POD_NAME → String
-
The reserved pod name for the startup banner.
final
- JETLEAF_APPLICATION_NAME → String
-
The property key used to override the Jetleaf application name.
final
- JETLEAF_ARGUMENT_POD_NAME → String
-
The reserved pod name for Jetleaf application arguments.
final
- LIFECYCLE_PROCESSOR_POD_NAME → String
-
The reserved pod name for the
LifecycleProcessor
within the Jetleaf context.final - MESSAGE_SOURCE_POD_NAME → String
-
The reserved pod name for the MessageSource.
final
- POD_NAME_GENERATOR_POD_NAME → String
-
The reserved pod name for the pod name generator.
final