refresh method

  1. @override
Future<void> refresh()
override

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:

  1. Prepare Refresh: Validate state, prepare internal structures
  2. Obtain Fresh Factory: Create new pod factory instance
  3. Prepare Factory: Register core pods and configuration
  4. Post-process Factory: Apply pod factory post-processors
  5. Register Processors: Register pod-aware processors
  6. Initialize Message Source: Set up internationalization
  7. Init Application Event Bus: Initialize event system
  8. Register Listeners: Register application event listeners
  9. Instantiate Singletons: Create non-lazy singleton pods
  10. 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 configured
  • PodDefinitionException 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();
    }
  });
}