initialize abstract method

void initialize(
  1. T applicationContext
)

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();
}

Initialize the given applicationContext before refresh.

This method is called during the context refresh process, after the context is created but before any pods are instantiated. It provides an opportunity to programmatically configure the context.

Parameters:

  • applicationContext: The application context being initialized

Typical Operations:

  • Register additional pod definitions
  • Configure the environment or property sources
  • Set up application-specific context attributes
  • Validate configuration state
  • Register custom factories or processors

Example:

@override
void initialize(GenericApplicationContext context) {
  // Access and modify the pod factory
  final factory = context.getPodFactory();
  factory.registerSingleton('appMetadata', AppMetadata(
    version: '1.0.0',
    buildTime: DateTime.now()
  ));
  
  // Configure based on environment
  final env = context.getEnvironment();
  if (env.acceptsProfiles({'testing'})) {
    factory.registerSingleton('emailService', MockEmailService());
  } else {
    factory.registerSingleton('emailService', ProductionEmailService(
      apiKey: env.getRequiredProperty('email.api.key')
    ));
  }
  
  // Set custom context attributes
  context.setAttribute('deployment.region', 'us-west-2');
}

Error Handling:

Exceptions thrown from initializers will typically prevent the context from refreshing successfully. Initializers should handle their own exceptions or throw meaningful error messages.

Implementation

void initialize(T applicationContext);