refresh method
Refreshes this application context:
- Loads or reloads pod definitions.
- Instantiates singletons.
- Initializes application components.
- Publishes lifecycle events.
The refresh process is the core initialization sequence that transitions the context from configured to active state.
Refresh Sequence:
- Prepare Refresh: Validate state, prepare internal structures
- Obtain Fresh Factory: Create new pod factory instance
- Prepare Factory: Register core pods and configuration
- Post-process Factory: Apply pod factory post-processors
- Register Processors: Register pod-aware processors
- Initialize Message Source: Set up internationalization
- Init Application Event Bus: Initialize event system
- Register Listeners: Register application event listeners
- Instantiate Singletons: Create non-lazy singleton pods
- Finish Refresh: Complete initialization, publish events
Example:
try {
await context.refresh();
print('Context refreshed successfully');
print('Active pods: ${context.getPodDefinitionNames()}');
} on PodDefinitionException catch (e) {
print('Pod configuration error: ${e.message}');
exit(1);
} on Exception catch (e) {
print('Context refresh failed: $e');
exit(1);
}
Throws:
IllegalStateException
if the context has not been properly configuredPodDefinitionException
if pod definitions are invalid- Various runtime exceptions if initialization fails
Important:
This method should be called exactly once during the context lifecycle.
Implementation
@override
Future<void> refresh() async {
return synchronized(_lock, () async {
// Prepare this context for refreshing.
await prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListablePodFactory podFactory = await doGetFreshPodFactory();
this.podFactory = podFactory;
// Prepare the pod factory for use in this context.
await preparePodFactory();
try {
// Allows post-processing of the pod factory in context subclasses.
await postProcessPodFactory();
// Invoke factory processors registered as pods in the context.
await invokePodFactoryPostProcessors();
// Register pod processors that intercept pod creation.
await registerPodAwareProcessors();
// Initialize message source for this context.
await initMessageSource();
// Initialize event multicaster for this context.
await initApplicationEventBus();
// Initialize other special pods in specific context subclasses.
await completeRefresh();
// Check for listener pods and register them.
await registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
await finishPodFactoryInitialization();
// Last step: publish corresponding event.
await finishRefresh();
} on PodException catch (ex) {
if (logger.getIsWarnEnabled()) {
logger.warn("Exception encountered during context initialization - cancelling refresh attempt: $ex");
}
// Destroy already created singletons to avoid dangling resources.
await destroyPods();
// Reset 'active' flag.
await cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
} finally {
// Reset common introspection caches ...
await resetCommonCaches();
}
});
}