A bean with work to do once, right after the application has started.
Database migrations, filling a cache, an import job: work that needs
the beans ready, should run once per start, and should not hold up the
creation of other beans. Implement this interface on a @Component()
and SpringApplication calls run after every bean is created. A
program that is only a job (a command-line tool) does all its work
in a runner.
Runners, together with CommandLineRunners, are called one after
another, sorted by @Order or Ordered. A runner that returns a
Future is finished before the next one starts. A runner that throws,
or whose Future fails, fails the start: the failure is reported, the
context is closed and the error is rethrown. The generated
runApplication returns before asynchronous runners are done;
runApplicationAsync completes after the last one. A bean that
implements both interfaces is called as both.
@Component()
@Order(1)
class Migrations implements ApplicationRunner {
Migrations(this.db);
final Database db;
@override
Future<void> run(ApplicationArguments args) => db.migrate(); // awaited
}
@Component()
@Order(2)
class Warmup implements ApplicationRunner { // starts after Migrations
@override
void run(ApplicationArguments args) { ... }
}
Spring (Java):
@Component @Order(1)
class Migrations implements ApplicationRunner {
public void run(ApplicationArguments args) { db.migrate(); }
}
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
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
run(
ApplicationArguments args) → FutureOr< void> - Does the startup work. Called once, after every bean is created, with the parsed program arguments.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited