Code that prepares the context before its beans are created.
Some setup cannot be done with beans because it must happen before
any bean exists: a property source that every bean should see, a
ready-made object registered as a bean, a post-processor. Pass
initializers to SpringApplication(initializers: [...]); they run
after the context is created and before its beans are, sorted by
Ordered (unordered ones last). Test tools are the main users: they
add test properties and replace beans this way. Applications rarely
need one.
// Point the clients at the local test servers, before any bean reads
// `api.url`.
final class TestServers implements ApplicationContextInitializer, Ordered {
@override
int get order => Ordered.highestPrecedence;
@override
void initialize(ConfigurableApplicationContext context) =>
context.environment.propertySources.addFirst(
MapPropertySource('servers', {'api.url': 'http://localhost:8081'}),
);
}
SpringApplication([appBeans], initializers: [
TestServers(),
ApplicationContextInitializer.of((context) => log.info(context.id)),
]);
Spring (Java):
class TestServers implements
ApplicationContextInitializer<ConfigurableApplicationContext>, Ordered {
public void initialize(ConfigurableApplicationContext context) { ... }
}
application.addInitializers(new TestServers(), context -> ...);
Constructors
- ApplicationContextInitializer.of(void initialize(ConfigurableApplicationContext context), {int order})
-
An initializer that runs
initialize, placed byorder(last by default); the short form of a class.constfactory
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
-
initialize(
ConfigurableApplicationContext context) → void -
Prepares
context, which has its environment and bean definitions but no beans yet. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited