@DependsOn
@DependsOn([...]) names beans that must be created before this one and destroyed after it.
Why
The container already orders beans by their injected dependencies: if A takes B in its constructor, B is created first and destroyed last. You do not need @DependsOn for that.
Use it only for a dependency the container cannot see: a bean that relies on another bean's side effect without injecting it. Examples: a schema that another bean creates in the database, a global value that another bean sets up, a file that another bean writes.
Every name must exist. refresh() checks that before any bean is created.
Example
import 'package:spring_core_dart/spring_core_dart.dart';
// Creates the database tables when it starts.
@Component('schema')
class Schema {
@PostConstruct()
void create() {
// CREATE TABLE ...
}
}
// Reads the tables, but never calls Schema. Without @DependsOn the
// container could create it first.
@Component()
@DependsOn(['schema'])
class Migrations {
@PostConstruct()
void run() {
// runs after the `schema` bean was created and initialized
}
}
Parameters
| Name | Type | Default | Meaning |
|---|---|---|---|
value (positional) |
List<String> |
required | Names of the beans created before this one. |
In Spring (Java)
@Component
@DependsOn("schema")
class Migrations { ... }
Differences that matter: Spring takes a single name or an array; here it is always a list. A missing name fails refresh() before any bean is created.
See also
- @PostConstruct, @PreDestroy
- @Bean
- BeanRegistrar (
dependsOn)