Interface ComponentDefinition<C>

Type Parameters:
C - The declared type of the component.
All Known Subinterfaces:
ComponentDefinition.ComponentCreator<C>
All Known Implementing Classes:
AbstractComponent, InstantiatedComponentDefinition, LazyInitializedComponentDefinition

public sealed interface ComponentDefinition<C> permits ComponentDefinition.ComponentCreator<C>
Defines the structure of a Component that is available in the Configuration of the application or one of its Modules.

Components are identified by a combination of their declared type and a name. The declared type is generally an interface that all implementations are expected to implement. The name can be any non-empty string value that identifies a particular instance of a component. If the name is not relevant, for example because only a single instance is expected to be present, it can be omitted in the definition, in which case it will default to the simple class name of the declared Component type.

For example, to create a component of type MyComponentInterface with an implementation that has a dependency, it would look as follows:


 ComponentDefinition.ofType(MyComponentInterface.class)
                    .withBuilder(config -> new MyComponentImplementation(config.getComponent(MyDependency.class)))
                    .onStart(0, MyComponentImplementation::start)
                    .onShutdown(0, MyComponentImplementation::shutdown)
 

Alternatively, you can specify a name to make multiple instances of the same component in the same configuration:


     ComponentDefinition.ofTypeAndName(MyComponentInterface.class, "MyName")
                        .withBuilder(config -> ...)
 

If an instance of the component is already constructed, it is more efficient to register it directly


     ComponentDefinition.ofTypeAndName(MyComponentInterface.class, "MyName")
                        .withInstance(myPreCreatedInstance)
 
Since:
5.0.0
Author:
Allard Buijze
  • Method Details

    • ofType

      static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofType(@Nonnull Class<C> type)
      Starts defining a component with given declared type. To distinguish between different instances of the same type, consider using ofTypeAndName(Class, String) instead. In case the component carries a generic type, consider using ofType(TypeReference) instead to prevent casting errors during registration of the component.

      Either withBuilder(...) or withInstance(...) must be called on the result of this invocation to create a valid ComponentDefinition instance.

      Type Parameters:
      C - The declared type of the component.
      Parameters:
      type - The declared type of the component.
      Returns:
      A builder to complete the creation of a ComponentDefinition.
      See Also:
    • ofTypeAndName

      static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofTypeAndName(@Nonnull Class<C> type, @Nullable String name)
      Starts defining a component with given declared type and name. If only a single instance of a component is expected to be used, consider using ofType(Class) instead. In case the component carries a generic type, consider using ofTypeAndName(TypeReference, String) instead to prevent casting errors during registration of the component.
      Type Parameters:
      C - The declared type of this component.
      Parameters:
      type - The declared type of this component.
      name - The name of this component. Use null when there is no name or use ofType(Class) instead.
      Returns:
      A builder to complete the creation of a ComponentDefinition.
    • ofType

      static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofType(@Nonnull TypeReference<C> type)
      Starts defining a component with given declared type. To distinguish between different instances of the same type, consider using ofTypeAndName(TypeReference, String) instead.

      This method is a convenience overload of ofTypeAndName(Class, String) that can accept a type reference so components with generic types can be registered without casting errors. If your component does not have a generic type, consider using ofTypeAndName(Class, String) instead.

      Either withBuilder(...) or withInstance(...) must be called on the result of this invocation to create a valid ComponentDefinition instance.

      Type Parameters:
      C - The declared type of the component.
      Parameters:
      type - The declared type of the component.
      Returns:
      A builder to complete the creation of a ComponentDefinition.
      See Also:
    • ofTypeAndName

      static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofTypeAndName(@Nonnull TypeReference<C> type, @Nullable String name)
      Starts defining a component with given declared type and name. If only a single instance of a component is expected to be used, consider using ofType(TypeReference) instead.

      This method is a convenience overload of ofTypeAndName(Class, String) that can accept a type reference so components with generic types can be registered without casting errors. If your component does not have a generic type, consider using ofTypeAndName(Class, String) instead.

      Type Parameters:
      C - The declared type of this component.
      Parameters:
      type - The declared type of this component.
      name - The name of this component.
      Returns:
      A builder to complete the creation of a ComponentDefinition.
    • onStart

      ComponentDefinition<C> onStart(int phase, @Nonnull ComponentLifecycleHandler<C> handler)
      Registers the given handler to be invoked during the startup lifecycle of the application in the given phase.
      Parameters:
      phase - The phase in which to invoke the given handler.
      handler - The start handler to execute on the component.
      Returns:
      A ComponentDefinition with the start handler defined.
    • onStart

      default ComponentDefinition<C> onStart(int phase, @Nonnull Consumer<C> handler)
      Registers the given handler to be invoked during the startup lifecycle of the application in the given phase.
      Parameters:
      phase - The phase in which to invoke the given handler.
      handler - The start handler to execute on the component.
      Returns:
      A ComponentDefinition with the start handler defined.
    • onStart

      default ComponentDefinition<C> onStart(int phase, @Nonnull BiConsumer<Configuration,C> handler)
      Registers the given handler to be invoked during the startup lifecycle of the application in the given phase.
      Parameters:
      phase - The phase in which to invoke the given handler.
      handler - The start handler to execute on the component.
      Returns:
      A ComponentDefinition with the start handler defined.
    • onShutdown

      ComponentDefinition<C> onShutdown(int phase, @Nonnull ComponentLifecycleHandler<C> handler)
      Registers the given handler to be invoked during the shutdown lifecycle of the application in the given phase.
      Parameters:
      phase - The phase in which to invoke the given handler.
      handler - The action to execute on the component.
      Returns:
      A ComponentDefinition with the shutdown handler defined.
    • onShutdown

      default ComponentDefinition<C> onShutdown(int phase, @Nonnull Consumer<C> handler)
      Registers the given handler to be invoked during the shutdown lifecycle of the application in the given phase.
      Parameters:
      phase - The phase in which to invoke the given handler.
      handler - The action to execute on the component.
      Returns:
      A ComponentDefinition with the shutdown handler defined.
    • onShutdown

      default ComponentDefinition<C> onShutdown(int phase, @Nonnull BiConsumer<Configuration,C> handler)
      Registers the given handler to be invoked during the shutdown lifecycle of the application in the given phase.
      Parameters:
      phase - The phase in which to invoke this handler.
      handler - The action to execute on the component.
      Returns:
      A ComponentDefinition with the shutdown handler defined.
    • rawType

      Class<C> rawType()
      Returns the given type as a Class of this ComponentDefinition, set on ofType(Class) or ofTypeAndName(Class, String).
      Returns:
      The given type as a Class of this ComponentDefinition.
    • name

      @Nullable String name()
      Returns the given name of this ComponentDefinition, set on ofTypeAndName(Class, String).
      Returns:
      The given name of this ComponentDefinition.