@Import

@Import([...]) adds beans that the component scan does not find: configuration classes from elsewhere, or code that registers beans.

Why

@ComponentScan finds the beans in one directory and below. Use @Import on a configuration class when you need something else:

The generator creates registrars and selectors with their no-argument constructor.

Imports share the importing class's conditions. If the importing class is skipped (by @Profile or a condition), what it imports is skipped too.

Example

import 'package:spring_core_dart/spring_core_dart.dart';
// Declared in another package, outside the scanned directory.
import 'package:legacy_billing/legacy_billing.dart' show LegacyBillingConfig;

class Mailer {
  Mailer(this.host);
  final String host;
}

// Registers a mailer only when a mail host is configured.
class MailRegistrar implements BeanRegistrar {
  const MailRegistrar();

  @override
  void register(BeanRegistry registry, Environment environment) {
    final host = environment.getProperty('mail.host');
    if (host != null) {
      registry.registerBean<Mailer>((_) => Mailer(host), name: 'mailer');
    }
  }
}

// Only for production: the imports are skipped in other profiles too.
@Configuration()
@Profile(['prod'])
@Import([LegacyBillingConfig, MailRegistrar])
class ProductionConfig {}

Parameters

Name Type Default Meaning
value (positional) List<Type> required Configuration or component classes, BeanRegistrars, ImportSelectors and ImportBeanDefinitionRegistrars.

In Spring (Java)

@Configuration
@Profile("prod")
@Import({LegacyBillingConfig.class, MailRegistrar.class})
class ProductionConfig {}

Differences that matter:

See also