ApplicationContext class abstract
The central interface for a JetLeaf application context.
An ApplicationContext is the core container that provides:
- Dependency Injection: Access to managed pods (beans) via
ListablePodFactory
andHierarchicalPodFactory
- Environment Management: Configuration and profile handling via
EnvironmentCapable
- Internationalization: Message resolution and localization via MessageSource
- Event System: Application-wide event publication through publishEvent
- Lifecycle Management: Context state tracking and lifecycle operations
This interface serves as the primary entry point for configuring, bootstrapping, and interacting with a JetLeaf application at runtime.
Core Responsibilities:
- Container Management: Hosts and manages the complete dependency injection container
- Configuration Centralization: Provides unified access to application configuration
- Lifecycle Coordination: Manages context lifecycle from startup to shutdown
- Event Distribution: Facilitates loose coupling through event-driven architecture
- Resource Access: Provides access to framework and application resources
Basic Usage Example:
void main() async {
// Create and configure application context
final context = MyApplicationContext();
// Initialize the context (loads pods, processes configurations)
await context.refresh();
// Publish application startup event
context.publishEvent(ApplicationStartedEvent(context));
// Access context information
print("Application '${context.getApplicationName()}' started at: ${context.getStartTime()}");
print("Context ID: ${context.getId()}");
// Use the context throughout application lifetime
final userService = context.getPod<UserService>();
await userService.initialize();
// Properly close context on shutdown
await context.close();
}
Context Hierarchy:
Application contexts can form parent-child relationships where child contexts inherit configuration and can delegate pod lookups to their parents. This enables:
- Modular Applications: Separate contexts for different application modules
- Testing Scenarios: Isolated test contexts with shared parent services
- Multi-tenant Systems: Tenant-specific contexts with common infrastructure
Thread Safety:
Implementations should be thread-safe for concurrent access, particularly for read operations like pod retrieval and configuration access. Lifecycle operations (refresh, start, stop, close) should be properly synchronized.
See also:
- ConfigurableApplicationContext for lifecycle management and configuration
Environment
for environment and property management- MessageSource for internationalization support
- ApplicationEvent for event system integration
- Implemented types
- Implementers
Constructors
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
-
containsDefinition(
String name) → bool -
Returns
true
if this registry contains a pod with the givenname
.inherited -
containsPod(
String podName) → Future< bool> -
Checks if a pod with the given name is registered in the factory.
inherited
-
containsType(
Class type, [bool allowPodProviderInit = false]) → Future< bool> -
Checks if the factory contains a pod of the specified type.
inherited
-
findAllAnnotationsOnPod<
A> (String podName, Class< A> type) → Future<Set< A> > -
Finds all annotations of the specified type on a pod.
inherited
-
findAnnotationOnPod<
A> (String podName, Class< A> type) → Future<A?> -
Finds a specific annotation on a pod.
inherited
-
get<
T> (Class< T> type, [List<ArgumentValue> ? args]) → Future<T> -
Retrieves a pod instance by its type with optional arguments.
inherited
-
getAliases(
String podName) → List< String> -
Retrieves all alias names for the specified pod.
inherited
-
getApplicationName(
) → String - Returns the name of this application.
-
getDefinitionNames(
) → List< String> -
Returns a list of all pod names currently registered.
inherited
-
getDisplayName(
) → String - Returns the display name of this application context.
-
getEnvironment(
) → Environment -
🫘 Returns the
Environment
associated with the current context. -
getId(
) → String - Returns a unique identifier for this application context.
-
getMainApplicationClass(
) → Class< Object> - Returns the main application class.
-
getMessage(
String code, {List< Object> ? args, Locale? locale, String? defaultMessage}) → String -
Retrieve a message for the given code.
inherited
-
getNamedObject(
String podName, [List< ArgumentValue> ? args]) → Future<Object> -
Retrieves a pod as a generic Object by its name with optional arguments.
inherited
-
getNumberOfPodDefinitions(
) → int -
Returns the total number of pods registered.
inherited
-
getObject(
Class< Object> type, [List<ArgumentValue> ? args]) → Future<Object> -
Retrieves a pod as a generic Object by its type with optional arguments.
inherited
-
getPackageName(
) → String -
Represents an abstraction for identifying the package that an object,
resource, or service belongs to.
inherited
-
getParent(
) → ApplicationContext? - Returns the parent context, if any.
-
getPod<
T> (String podName, [List< ArgumentValue> ? args, Class<T> ? type]) → Future<T> -
Retrieves a pod instance by its name with optional arguments.
inherited
-
getPodClass(
String podName) → Future< Class> -
Retrieves the Class object for the specified pod name.
inherited
-
getPodFactory(
) → ConfigurableListablePodFactory -
🫘 Returns the underlying
ConfigurableListablePodFactory
. -
getPodNames(
Class type, {bool includeNonSingletons = false, bool allowEagerInit = false}) → Future< List< String> > -
Retrieves all pod names for pods of the specified type.
inherited
-
getPodNamesForAnnotation<
A> (Class< A> type) → Future<List< String> > -
Retrieves pod names for pods annotated with the specified annotation type.
inherited
-
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.
inherited
-
getPodsWithAnnotation<
A> (Class< A> type) → Future<Map< String, Object> > -
Retrieves pods with the specified annotation as a map of name to instance.
inherited
-
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.
inherited
-
getStartTime(
) → DateTime - Returns the startup time of this context as a DateTime.
-
isActive(
) → bool - Returns whether this application context is currently active.
-
isClosed(
) → bool - Returns whether this application context has been closed.
-
isPrototype(
String podName) → Future< bool> -
Checks if the specified pod is configured as a prototype.
inherited
-
isSingleton(
String podName) → Future< bool> -
Checks if the specified pod is configured as a singleton.
inherited
-
isTypeMatch(
String name, Class typeToMatch, [bool allowPodProviderInit = false]) → Future< bool> -
Checks if the pod with the specified name matches the given type.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
publishEvent(
ApplicationEvent event) → Future< void> -
Publishes the given
event
to all registered ApplicationEventListeners. -
register(
PodRegistrar registrar) → void -
Registers a
registrar
that declares pods into this registry.inherited -
registerPod<
T> (Class< T> podClass, {Consumer<Spec< ? customizer, String? name}) → voidT> > -
Registers a pod of type
T
in the registry.inherited -
resolveDependency(
DependencyDescriptor descriptor, [Set< String> ? autowiredPods]) → Future<Object?> -
Resolves a dependency based on the provided descriptor.
inherited
-
setEnvironment(
Environment environment) → void -
Sets the
Environment
that this component runs in.inherited -
supports(
ApplicationType applicationType) → bool -
Returns whether this context supports the given
applicationType
. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited