Configuration
Once ApplicationConfigurer#build() is called, it produces an AxonConfiguration.
This interface extends Configuration, which provides read-only access to all components that were registered during configuration, whether registered directly as a Component, wrapped by a ComponentDecorator, contributed by a ConfigurationEnhancer or Module, or produced on demand by a ComponentFactory.
Retrieving components
The primary method to retrieve a component is getComponent(Class<C> type):
import org.axonframework.common.configuration.AxonConfiguration;
import org.axonframework.eventsourcing.configuration.EventSourcingConfigurer;
import org.axonframework.messaging.commandhandling.gateway.CommandGateway;
class ComponentRetrievalExample {
void retrieveComponent() {
AxonConfiguration configuration = EventSourcingConfigurer.create()
// registration omitted
.build();
configuration.start();
CommandGateway gateway = configuration.getComponent(CommandGateway.class);
}
}
When a component is registered under a generic type (for example Repository<MyId, MyEntity>), use the
TypeReference-based overload instead of Class so the full generic type information is preserved:
import org.axonframework.common.TypeReference;
import org.axonframework.common.configuration.AxonConfiguration;
import org.axonframework.eventsourcing.configuration.EventSourcingConfigurer;
import org.axonframework.modelling.repository.Repository;
class TypeReferenceRetrievalExample {
void retrieveGenericComponent() {
AxonConfiguration configuration = EventSourcingConfigurer.create()
// registration omitted
.build();
configuration.start();
Repository<MyId, MyEntity> repository =
configuration.getComponent(new TypeReference<Repository<MyId, MyEntity>>() {
});
}
}
Every Class-based retrieval method below has a matching TypeReference-based overload that behaves the same way.
Additional retrieval methods let you handle optional components and named components:
getOptionalComponent(Class<C>)/getOptionalComponent(TypeReference<C>)-
Returns an
Optional, useful when a component may not have been registered. getComponent(Class<C>, String name)/getComponent(TypeReference<C>, String name)-
Returns the component registered under the given type and name. Used when multiple components of the same type exist (for example, multiple
EventProcessorinstances). getOptionalComponent(Class<C>, String name)/getOptionalComponent(TypeReference<C>, String name)-
Returns an
Optionalfor the component registered under the given type and name. getComponent(Class<C>, Supplier<C> defaultImpl)-
Returns the component registered under the given type, falling back to
defaultImplwhen none is registered. The default is then registered as the component for that type, so later calls return the same instance. getComponent(Class<C>, String name, Supplier<C> defaultImpl)-
Same as
getComponent(Class<C>, Supplier<C>), scoped to a specific component name. hasComponent(Class<?>)/hasComponent(TypeReference<?>)-
Returns
truewhen a component is present. Useful inConfigurationEnhancerimplementations to apply decorators conditionally. hasComponent(Class<?>, String name)/hasComponent(TypeReference<?>, String name)-
Returns
truewhen a component is present under the given type and name. getComponents(Class<C>)-
Returns a
Map<String, C>of all registered components of a given type. The map may contain anullkey for the unnamed component andStringkeys for named components. getModuleConfiguration(String name)andgetModuleConfigurations()-
Return the
Configurationinstances produced by registeredModules. These give access to components that are otherwise encapsulated within a module. getParent()-
Returns the parent
Configuration, ornullif none exists. Components can use this to build hierarchical lookups that prefer a component from a child configuration over one from a parent configuration.
When running with the Spring Boot Starter, you typically never call these retrieval methods yourself: Axon components are exposed as Spring beans and can be injected with @Autowired or constructor injection like any other bean.
This works because the SpringComponentRegistry also acts as a BeanFactoryPostProcessor, bridging bean lookups back to the underlying Configuration.
See Implementation details for how this bridge works in both directions.
|
|
Starting and stopping
AxonConfiguration is the specialized form of Configuration returned by ApplicationConfigurer#build().
Next to the read-only component access Configuration provides, it adds two lifecycle operations that activate (and later deactivate) everything that was configured:
start()-
Invokes all registered startup handlers, in the phase order described under component lifecycle. After this call, all messaging infrastructure is active.
shutdown()-
Invokes all registered shutdown handlers, in reverse phase order. Call this when your application exits.
import org.axonframework.common.configuration.AxonConfiguration;
import org.axonframework.eventsourcing.configuration.EventSourcingConfigurer;
class StartStopExample {
void run() {
AxonConfiguration configuration = EventSourcingConfigurer.create().build();
configuration.start();
// ... application runs ...
configuration.shutdown();
}
}
The ApplicationConfigurer#start() convenience method combines build() and start().
When you configure your application fully declaratively, through MessagingConfigurer, ModellingConfigurer, or EventSourcingConfigurer, invoking start() is mandatory.
Without it, none of the registered startup handlers run and your messaging infrastructure never becomes active.
Likewise, call shutdown() when your application exits so components can release resources and stop gracefully.
This is not something you need to worry about when a dependency injection framework manages the AxonConfiguration for you.
Axon’s Spring integration, for example, ties start() and shutdown() into the Spring application context lifecycle, so you never have to invoke them yourself.