ApplicationContextInitializer<T extends ConfigurableApplicationContext> class abstract

Strategy interface for initializing an ApplicationContext before it is refreshed.

Application context initializers provide a hook for programmatic configuration of the context before the refresh process begins. They are particularly useful for:

Common Use Cases:

  • Programmatic Configuration: Register pods or adjust settings via code
  • Environment Setup: Configure profiles or property sources
  • Custom Validation: Validate context configuration before refresh
  • Integration Setup: Prepare integration with external systems
  • Feature Flags: Enable/disable features based on configuration

Execution Context:

  • Initializers are called after the context is created
  • But before the pod factory is refreshed
  • In the order defined by Ordered or @Order annotation
  • All initializers are called even if one fails

Example:

class DatabaseInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
  @override
  void initialize(GenericApplicationContext context) {
    // Configure database settings before refresh
    final env = context.getEnvironment();
    
    if (env.acceptsProfiles({'cloud'})) {
      // Use cloud database configuration
      context.getPodFactory().registerSingleton(
        'dataSource',
        CloudDataSource(env.getProperty('cloud.db.url'))
      );
    } else {
      // Use local database configuration
      context.getPodFactory().registerSingleton(
        'dataSource', 
        LocalDataSource(env.getProperty('local.db.path'))
      );
    }
    
    logger.info('Database configuration initialized');
  }
}

class SecurityInitializer implements ApplicationContextInitializer<GenericApplicationContext> {
  @override
  void initialize(GenericApplicationContext context) {
    // Set up security configuration
    context.getPodFactory().registerSingleton(
      'securityConfig',
      SecurityConfig(
        enabled: context.getEnvironment().getProperty('security.enabled', bool.fromString, true),
        jwtSecret: context.getEnvironment().getRequiredProperty('security.jwt.secret')
      )
    );
  }
}

void main() async {
  final ctx = GenericApplicationContext();
  
  // Register initializers
  ctx.addApplicationContextInitializer(DatabaseInitializer());
  ctx.addApplicationContextInitializer(SecurityInitializer());
  
  // Initializers will be called during refresh
  await ctx.refresh();
}
Annotations
  • @Generic.new(ApplicationContextInitializer)

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

initialize(T applicationContext) → void
Strategy interface for initializing an ApplicationContext before it is refreshed.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited