
# @Import

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

## Why

[@ComponentScan](component-scan.md) finds the beans in one directory and below. Use `@Import` on a configuration class when you need something else:

- a configuration or component class outside the scanned directories (shared code, a legacy module);
- a [BeanRegistrar](bean-registrar.md), which registers beans in code;
- an `ImportSelector`, which returns more `BeanModule`s to load, chosen at startup;
- an `ImportBeanDefinitionRegistrar`, which registers full bean definitions.

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

```dart
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, `BeanRegistrar`s, `ImportSelector`s and `ImportBeanDefinitionRegistrar`s. |

## In Spring (Java)

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

Differences that matter:

- An `ImportSelector` returns `BeanModule`s, not class names, and receives no annotation metadata of the importing class.
- Registrars are created with their no-argument constructor. There is no reflection to pass the importing class's metadata.

## See also

- [@ComponentScan](component-scan.md)
- [BeanRegistrar](bean-registrar.md)
- [BeanModule](bean-module.md)
- [@Configuration](configuration.md)
- [@Conditional](conditional.md), [@Profile](profile.md)
