GenericApplicationContext constructor
Generic ApplicationContext implementation that holds a single internal
DefaultListablePodFactory
instance and does not assume a specific pod
definition format.
This class implements the ConfigurableApplicationContext interface to allow for convenient registration of pod definitions and classes as well as programmatic registration of singletons. It serves as a flexible base for various application context implementations in the JetLeaf framework.
Key Features:
- Programmatic Registration: Register pods and singletons via code
- Flexible Configuration: No assumptions about pod definition formats
- Lifecycle Management: Full context lifecycle support
- Parent Context Support: Hierarchical context relationships
- Post-Processing: Extensible processor architecture
Context Lifecycle:
- Construction: Context created with internal pod factory
- Registration: Pod definitions and singletons registered
- Refresh: Context initialized and pods instantiated
- Operation: Pods available for dependency injection
- Destruction: Context closed and resources cleaned up
Important Note:
The refresh method must be called exactly once after all pod definitions are registered and before any pod access attempts.
Usage Example:
void main() async {
// Create context
final context = GenericApplicationContext();
// Register pod definitions programmatically
final podDef = RootPodDefinition(type: Class<UserService>());
context.registerDefinition('userService', podDef);
// Register singleton instances
context.getPodFactory().registerSingleton(
'configService',
ConfigService()
);
// Refresh to initialize the context
await context.refresh();
// Use the context
final userService = context.getPod<UserService>('userService');
await userService.processUsers();
// Close context when done
await context.close();
}
Advanced Configuration:
// With custom pod factory
final customFactory = DefaultListablePodFactory();
customFactory.setAllowCircularReferences(true);
final context = GenericApplicationContext.withPodFactory(customFactory);
// With parent context for hierarchical lookup
final parentContext = GenericApplicationContext();
final childContext = GenericApplicationContext.withParent(parentContext);
Framework Integration:
This context does not support special pod definition formats by default. For contexts that read pod definitions from annotations, configuration files, or other external sources, consider using specialized subclasses like AnnotationConfigApplicationContext.
See also:
- AbstractApplicationContext for the base implementation
- ConfigurableApplicationContext for the configuration interface
DefaultListablePodFactory
for the internal pod factory- AnnotationConfigApplicationContext for annotation-based configuration
Creates a new GenericApplicationContext with default settings.
The context is created with a new DefaultListablePodFactory
and
no parent context. You must call refresh after registering all
pod definitions to initialize the context.
Example:
final context = GenericApplicationContext();
// Register pods...
await context.refresh();
Implementation
GenericApplicationContext() : this.all(null, null);