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
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
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceMandatory interface to be implemented by all implementations ofComponentDefinition.static interfaceRepresents an intermediate step in the creation of aComponentDefinition. -
Method Summary
Modifier and TypeMethodDescriptionname()Returns the given name of thisComponentDefinition, set onofTypeAndName(Class, String).static <C> ComponentDefinition.IncompleteComponentDefinition<C> Starts defining a component with given declaredtype.static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofType(TypeReference<C> type) Starts defining a component with given declaredtype.static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofTypeAndName(Class<C> type, String name) Starts defining a component with given declaredtypeandname.static <C> ComponentDefinition.IncompleteComponentDefinition<C> ofTypeAndName(TypeReference<C> type, String name) Starts defining a component with given declaredtypeandname.default ComponentDefinition<C> onShutdown(int phase, BiConsumer<Configuration, C> handler) Registers the givenhandlerto be invoked during the shutdown lifecycle of the application in the givenphase.default ComponentDefinition<C> onShutdown(int phase, Consumer<C> handler) Registers the givenhandlerto be invoked during the shutdown lifecycle of the application in the givenphase.onShutdown(int phase, ComponentLifecycleHandler<C> handler) Registers the givenhandlerto be invoked during the shutdown lifecycle of the application in the givenphase.default ComponentDefinition<C> onStart(int phase, BiConsumer<Configuration, C> handler) Registers the givenhandlerto be invoked during the startup lifecycle of the application in the givenphase.default ComponentDefinition<C> Registers the givenhandlerto be invoked during the startup lifecycle of the application in the givenphase.onStart(int phase, ComponentLifecycleHandler<C> handler) Registers the givenhandlerto be invoked during the startup lifecycle of the application in the givenphase.rawType()Returns the giventype as a Classof thisComponentDefinition, set onofType(Class)orofTypeAndName(Class, String).
-
Method Details
-
ofType
Starts defining a component with given declaredtype. To distinguish between different instances of the same type, consider usingofTypeAndName(Class, String)instead. In case the component carries a generic type, consider usingofType(TypeReference)instead to prevent casting errors during registration of the component.Either
withBuilder(...)orwithInstance(...)must be called on the result of this invocation to create a validComponentDefinitioninstance.- 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 declaredtypeandname. If only a single instance of a component is expected to be used, consider usingofType(Class)instead. In case the component carries a generic type, consider usingofTypeAndName(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. Usenullwhen there is no name or useofType(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 declaredtype. To distinguish between different instances of the same type, consider usingofTypeAndName(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 usingofTypeAndName(Class, String)instead.Either
withBuilder(...)orwithInstance(...)must be called on the result of this invocation to create a validComponentDefinitioninstance.- 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 declaredtypeandname. If only a single instance of a component is expected to be used, consider usingofType(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 usingofTypeAndName(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
Registers the givenhandlerto be invoked during the startup lifecycle of the application in the givenphase.- Parameters:
phase- The phase in which to invoke the givenhandler.handler- The start handler to execute on the component.- Returns:
- A
ComponentDefinitionwith the start handler defined.
-
onStart
Registers the givenhandlerto be invoked during the startup lifecycle of the application in the givenphase.- Parameters:
phase- The phase in which to invoke the givenhandler.handler- The start handler to execute on the component.- Returns:
- A
ComponentDefinitionwith the start handler defined.
-
onStart
Registers the givenhandlerto be invoked during the startup lifecycle of the application in the givenphase.- Parameters:
phase- The phase in which to invoke the givenhandler.handler- The start handler to execute on the component.- Returns:
- A
ComponentDefinitionwith the start handler defined.
-
onShutdown
Registers the givenhandlerto be invoked during the shutdown lifecycle of the application in the givenphase.- Parameters:
phase- The phase in which to invoke the givenhandler.handler- The action to execute on the component.- Returns:
- A
ComponentDefinitionwith the shutdown handler defined.
-
onShutdown
Registers the givenhandlerto be invoked during the shutdown lifecycle of the application in the givenphase.- Parameters:
phase- The phase in which to invoke the givenhandler.handler- The action to execute on the component.- Returns:
- A
ComponentDefinitionwith the shutdown handler defined.
-
onShutdown
Registers the givenhandlerto be invoked during the shutdown lifecycle of the application in the givenphase.- Parameters:
phase- The phase in which to invoke this handler.handler- The action to execute on the component.- Returns:
- A
ComponentDefinitionwith the shutdown handler defined.
-
rawType
Returns the giventype as a Classof thisComponentDefinition, set onofType(Class)orofTypeAndName(Class, String).- Returns:
- The given
type as a Classof thisComponentDefinition.
-
name
Returns the given name of thisComponentDefinition, set onofTypeAndName(Class, String).- Returns:
- The given name of this
ComponentDefinition.
-