Class SpringComponentRegistry

java.lang.Object
org.axonframework.extension.spring.config.SpringComponentRegistry
All Implemented Interfaces:
ComponentRegistry, DescribableComponent, org.springframework.beans.factory.config.BeanFactoryPostProcessor, org.springframework.beans.factory.config.BeanPostProcessor

@Internal public class SpringComponentRegistry extends Object implements org.springframework.beans.factory.config.BeanPostProcessor, org.springframework.beans.factory.config.BeanFactoryPostProcessor, ComponentRegistry
A ComponentRegistry implementation that connects into Spring's ecosystem by means of being a BeanPostProcessor, BeanFactoryPostProcessor, and InitializingBean.

By being a BeanPostProcessor, this ComponentRegistry can decorate any Spring bean that matches with decorators set in this ComponentRegistry or any ConfigurationEnhancer.

By being a BeanFactoryPostProcessor, this ComponentRegistry can return any component, regardless of whether it was registered with this ComponentRegistry, through a ConfigurationEnhancer, or comes from Spring's Application Context directly. The latter integration ensures that any Axon Framework component using the Configuration resulting from this ComponentRegistry can retrieve any bean that's available. The BeanFactory that's set through BeanFactoryPostProcessor.postProcessBeanFactory(ConfigurableListableBeanFactory) is also used to validate if this registery has a certain component.

Since:
4.6.0
Author:
Allard Buijze, Steven van Beelen
  • Constructor Details

    • SpringComponentRegistry

      @Internal public SpringComponentRegistry(@Nonnull org.springframework.beans.factory.ListableBeanFactory listableBeanFactory, @Nonnull SpringLifecycleRegistry lifecycleRegistry)
      Constructs a SpringComponentRegistry with the given listableBeanFactory. The listableBeanFactory is used to discover all beans of type ConfigurationEnhancer.
      Parameters:
      listableBeanFactory - The bean factory used to discover all beans of type ConfigurationEnhancer.
      lifecycleRegistry - The LifecycleRegistry used to initializes registered modules.
  • Method Details

    • registerComponent

      public <C> ComponentRegistry registerComponent(@Nonnull ComponentDefinition<? extends C> definition)
      Description copied from interface: ComponentRegistry
      Registers a Component based on the given definition.
      Specified by:
      registerComponent in interface ComponentRegistry
      Type Parameters:
      C - The declared type of the component.
      Parameters:
      definition - The definition of the component to register.
      Returns:
      The current instance of the Configurer for a fluent API.
    • registerDecorator

      public <C> ComponentRegistry registerDecorator(@Nonnull DecoratorDefinition<C,? extends C> definition)
      Description copied from interface: ComponentRegistry
      Registers a decorator based on the given definition.
      Specified by:
      registerDecorator in interface ComponentRegistry
      Type Parameters:
      C - The declared type of the component(s) to decorate.
      Parameters:
      definition - The definition of the decorator to apply to components.
      Returns:
      The current instance of the Configurer for a fluent API.
      See Also:
    • hasComponent

      public boolean hasComponent(@Nonnull Class<?> type, @Nullable String name, @Nonnull SearchScope searchScope)
      Description copied from interface: ComponentRegistry
      Check whether there is a Component registered with this Configurer for the given type and name combination.

      The given searchScope is used to define if the search only checks the current registry, only checks all ancestors, or checks both the current registry and all ancestors.

      Specified by:
      hasComponent in interface ComponentRegistry
      Parameters:
      type - The type of the Component to check if it exists, typically an interface.
      name - The name of the Component to check if it exists. Use null when there is no name or use ComponentRegistry.hasComponent(Class) instead.
      searchScope - The enumeration defining the search scope used to check if this registry has a Component.
      Returns:
      true when there is a Component registered under the given type and name combination, false otherwise.
    • registerEnhancer

      public ComponentRegistry registerEnhancer(@Nonnull ConfigurationEnhancer enhancer)
      Description copied from interface: ComponentRegistry
      Registers an ConfigurationEnhancer with this ComponentRegistry.

      An enhancer is able to invoke any of the methods on this ComponentRegistry, allowing it to add (sensible) defaults, decorate components, or replace components entirely.

      An enhancer's ConfigurationEnhancer.enhance(ComponentRegistry) method is invoked during the initialization phase when all components have been defined. This is right before the ComponentRegistry creates its Configuration.

      When multiple enhancers have been provided, their ConfigurationEnhancer.order() dictates the enhancement order. For enhancer with the same order, the order of execution is undefined.

      Specified by:
      registerEnhancer in interface ComponentRegistry
      Parameters:
      enhancer - The configuration enhancer to enhance ComponentRegistry during ApplicationConfigurer.build().
      Returns:
      The current instance of the Configurer for a fluent API.
    • registerModule

      public ComponentRegistry registerModule(@Nonnull Module module)
      Description copied from interface: ComponentRegistry
      Registers a Module with this registry.

      Note that a Module is able to access the components defined in this ComponentRegistry upon construction, but not vice versa. As such, the Module maintains encapsulation.

      Specified by:
      registerModule in interface ComponentRegistry
      Parameters:
      module - The module builder function to register.
      Returns:
      The current instance of the ComponentRegistry for a fluent API.
    • registerFactory

      public <C> ComponentRegistry registerFactory(@Nonnull ComponentFactory<C> factory)
      Description copied from interface: ComponentRegistry
      Registers a ComponentFactory with this registry.

      If the Configuration that will contain this registry does not have a component for a given Class and name combination, it will consult all registered component factories. Only if a given factory can produce the requested type will ComponentFactory.construct(String, Configuration) be invoked. When the factory decides to construct a new component, it will be stored in the Configuration for future reference to ensure it's not constructed again.

      Specified by:
      registerFactory in interface ComponentRegistry
      Type Parameters:
      C - The component type constructed by the given factory.
      Parameters:
      factory - The component factory to register.
      Returns:
      The current instance of the ComponentRegistry for a fluent API.
    • setOverridePolicy

      public ComponentRegistry setOverridePolicy(@Nonnull OverridePolicy overridePolicy)
      Description copied from interface: ComponentRegistry
      Sets the OverridePolicy for this ComponentRegistry.

      This policy dictates what should happen when components are registered with an identifier for which another component is already present.

      Specified by:
      setOverridePolicy in interface ComponentRegistry
      Parameters:
      overridePolicy - The override policy for components defined in this registry.
      Returns:
      The current instance of the Configurer for a fluent API.
    • disableEnhancerScanning

      public ComponentRegistry disableEnhancerScanning()
      Description copied from interface: ComponentRegistry
      Completely disables scanning for enhancers on the classpath through the ServiceLoader mechanism. Note that this may lead to missing framework functionality. It is recommended to disable specific enhancers through ComponentRegistry.disableEnhancer(Class) instead. Does not affect enhancers that are registered through the ComponentRegistry.registerEnhancer(ConfigurationEnhancer) method.
      Specified by:
      disableEnhancerScanning in interface ComponentRegistry
      Returns:
      The current instance of the Configurer for a fluent API.
    • disableEnhancer

      public ComponentRegistry disableEnhancer(@Nonnull String fullyQualifiedClassName)
      Description copied from interface: ComponentRegistry
      Disables the given ConfigurationEnhancer class from executing during the configuration initialization phase. This affects both enhancers registered through the ServiceLoader mechanism and those registered programmatically via ComponentRegistry.registerEnhancer(ConfigurationEnhancer).

      Only specific classes can be disabled, and class hierarchies are not taken into account. If the enhancer has already been invoked when this method is called, disabling will have no effect and a warning will be logged.

      This method is typically called from within another enhancer's ConfigurationEnhancer.enhance(ComponentRegistry) method to prevent subsequent enhancers from executing.

      If the class cannot be found on the classpath, a warning will be logged and the call will have no effect.

      Specified by:
      disableEnhancer in interface ComponentRegistry
      Parameters:
      fullyQualifiedClassName - The fully qualified class name of the enhancer to disable.
      Returns:
      The current instance of the Configurer for a fluent API.
    • disableEnhancer

      public ComponentRegistry disableEnhancer(Class<? extends ConfigurationEnhancer> enhancerClass)
      Description copied from interface: ComponentRegistry
      Disables the given ConfigurationEnhancer class from executing during the configuration initialization phase. This affects both enhancers registered through the ServiceLoader mechanism and those registered programmatically via ComponentRegistry.registerEnhancer(ConfigurationEnhancer).

      Only specific classes can be disabled, and class hierarchies are not taken into account. If the enhancer has already been invoked when this method is called, disabling will have no effect and a warning will be logged.

      This method is typically called from within another enhancer's ConfigurationEnhancer.enhance(ComponentRegistry) method to prevent subsequent enhancers from executing.

      Specified by:
      disableEnhancer in interface ComponentRegistry
      Parameters:
      enhancerClass - The class of the enhancer to disable.
      Returns:
      The current instance of the Configurer for a fluent API.
    • describeTo

      public void describeTo(@Nonnull ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.
    • postProcessBeanFactory

      public void postProcessBeanFactory(@Nonnull org.springframework.beans.factory.config.ConfigurableListableBeanFactory beanFactory) throws org.springframework.beans.BeansException
      Specified by:
      postProcessBeanFactory in interface org.springframework.beans.factory.config.BeanFactoryPostProcessor
      Throws:
      org.springframework.beans.BeansException
    • postProcessAfterInitialization

      public Object postProcessAfterInitialization(@Nonnull Object bean, @Nonnull String beanName) throws org.springframework.beans.BeansException
      Override from the BeanPostProcessor interface.

      This ensures that registered decorators or decorators registered through ConfigurationEnhancers are invoked for Spring beans that match Axon's type-and-name criteria for decoration.

      Will retrieve a ResolvableType from the given beanName from the ConfigurableListableBeanFactory set through postProcessBeanFactory(ConfigurableListableBeanFactory) (if contained in said bean factory). Doing so ensures we match decorators with the bean's registered type instead of the bean's concrete type.

      Generic type checks on the DecoratorDefinition and it's invocations are suppressed as we're dealing with wildcards. Furthermore, the DecoratorDefinition.CompletedDecoratorDefinition.matches(Component.Identifier) invocation ensures we validate if the DecoratorDefinition.CompletedDecoratorDefinition.decorate(Component) invocations is valid.

      Specified by:
      postProcessAfterInitialization in interface org.springframework.beans.factory.config.BeanPostProcessor
      Throws:
      org.springframework.beans.BeansException
    • configuration

      public Configuration configuration()
      Accessor method for the SpringAxonApplication to access this registry's Configuration.
      Returns:
      The Configuration constructed by this ComponentRegistry.