Interface DecoratorDefinition<C,D extends C>
- Type Parameters:
C- The declared type of the component.D- The instance type of the decorated component.
- All Known Subinterfaces:
DecoratorDefinition.CompletedDecoratorDefinition<C,D>
components in the Configuration of the
application or one of its Modules.
Decorators can wrap or replace the implementation of defined components based on their type and optionally their name. Decorators must return an instance that is an implementation of the declared type. Typically, they wrap another component to provide additional behavior.
To create a decorator for a component of type MyComponentInterface with an implementation that has a
dependency, it would look as follows:
DecoratorDefinition.forType(MyComponentInterface.class)
.with((config, name, delegate) -> new MyComponentWrapper(delegate, config.getComponent(MyDependency.class)))
.onStart(0, MyComponentWrapper::start)
.onShutdown(0, MyComponentWrapper::shutdown)
Alternatively, you can use:
DecoratorDefinition.forTypeAndName(MyComponentInterface.class, "MyName")
.with(config -> ...)
In this case, you need to only wrap a component with a given name.- Since:
- 5.0.0
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceDefines the behavior that all implementation ofDecoratorDefinitionmust provide.static interfaceRepresents an intermediate phase in the creation of aDecoratorDefinition. -
Method Summary
Modifier and TypeMethodDescriptionstatic <C> DecoratorDefinition.PartialDecoratorDefinition<C> Initiates the configuration of a decorator forcomponentswith the giventype, which must correspond with the declared type of these components.static <C> DecoratorDefinition.PartialDecoratorDefinition<C> forTypeAndName(Class<C> type, String name) Initiates the configuration of a decorator for acomponentwith the giventypeandname, which must correspond with the declared type of these components.default DecoratorDefinition<C, D> onShutdown(int phase, Consumer<D> handler) Registers the givenhandlerto be registered with the application's lifecycle during shutdown for this decorator.onShutdown(int phase, ComponentLifecycleHandler<D> handler) Registers the givenhandlerto be registered with the application's lifecycle during shutdown for this decorator.default DecoratorDefinition<C, D> Registers the givenhandlerto be registered with the application's lifecycle during startup for this decorator.onStart(int phase, ComponentLifecycleHandler<D> handler) Registers the givenhandlerto be registered with the application's lifecycle during startup for this decorator.order(int order) Sets theorderin which this decorator will be invoked on a component relative to other decorators.
-
Method Details
-
forType
Initiates the configuration of a decorator forcomponentswith the giventype, which must correspond with the declared type of these components.If multiple components have been defined for this type, they all are subject to decoration under this definition. To decorate only a specific component, consider using
forTypeAndName(Class, String).- Type Parameters:
C- The declared type of the components to decorate.- Parameters:
type- The class of the declared type of the components to decorate.- Returns:
- A builder for further configuration of this decorator definition.
-
forTypeAndName
static <C> DecoratorDefinition.PartialDecoratorDefinition<C> forTypeAndName(@Nonnull Class<C> type, @Nonnull String name) Initiates the configuration of a decorator for acomponentwith the giventypeandname, which must correspond with the declared type of these components.The decorator is only invoked if such component with such name is defined in the component registry where this decorator is registered. If multiple components for this type have been defined, and all are subject to decoration, consider using
forType(Class).- Type Parameters:
C- The declared type of the components to decorate.- Parameters:
type- The class of the declared type of the components to decorate.name- The name of the component to decorate.- Returns:
- A builder for further configuration of this decorator definition.
-
order
Sets theorderin which this decorator will be invoked on a component relative to other decorators.The relative order of decorators with the same
orderis undefined.In absence of configuration, the order is
0.- Parameters:
order- The relative order in which to invoke this decorator.- Returns:
- This
DecoratorDefinitionfur fluent API.
-
onStart
Registers the givenhandlerto be registered with the application's lifecycle during startup for this decorator.The handler will be invoked in the given startup
phasefor each component that has been decorated.- Parameters:
phase- The phase in which the handler must be invoked.handler- The handler to invoke.- Returns:
- This
DecoratorDefinitionfur fluent API.
-
onStart
Registers the givenhandlerto be registered with the application's lifecycle during startup for this decorator.The handler will be invoked in the given startup
phasefor each component that has been decorated.- Parameters:
phase- The phase in which the start handler must be invoked.handler- The handler to invoke.- Returns:
- This
DecoratorDefinitionfur fluent API.
-
onShutdown
Registers the givenhandlerto be registered with the application's lifecycle during shutdown for this decorator.The handler will be invoked in the given shutdown
phasefor each component that has been decorated.- Parameters:
phase- The phase in which the shutdown handler must be invoked.handler- The handler to invoke.- Returns:
- This
DecoratorDefinitionfur fluent API.
-
onShutdown
Registers the givenhandlerto be registered with the application's lifecycle during shutdown for this decorator.The handler will be invoked in the given shutdown
phasefor each component that has been decorated.- Parameters:
phase- The phase in which the shutdown handler must be invoked.handler- The handler to invoke.- Returns:
- This
DecoratorDefinitionfur fluent API.
-