Interface TenantComponentFactory<T>
- Type Parameters:
T- the type of component this factory creates
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Creates and destroys the per-tenant instances held by a
TenantComponentProvider.
Implement this interface to define how a tenant-scoped component (such as a repository, service, or datasource) is
built for a given tenant. The provider invokes create(TenantDescriptor) lazily,
the first time a component is needed for a tenant, and destroy(TenantDescriptor, Object) when that tenant is
removed. The default destroy closes components that implement AutoCloseable. Override it for custom
cleanup.
Example usage:
// Build a tenant-specific repository from the tenant's datasource:
TenantComponentFactory<CourseRepository> factory =
tenant -> new JdbcCourseRepository(dataSourceFor(tenant));
- Since:
- 5.3.0
- Author:
- Theo Emanuelsson, Jan Galinski, Laura Devriendt
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptioncreate(TenantDescriptor tenant) Creates a component instance for the giventenant.default voiddestroy(TenantDescriptor tenant, T component) Destroys the givencomponentwhen itstenantis removed.
-
Method Details
-
create
Creates a component instance for the giventenant.- Parameters:
tenant- the tenant to create the component instance for- Returns:
- a new component instance for the given
tenant
-
destroy
Destroys the givencomponentwhen itstenantis removed.The default implementation closes components that implement
AutoCloseable. Failures to close are logged and suppressed, so the remaining tenants' components are still cleaned up. Override this method to release other tenant-scoped resources (for example database connections or caches).- Parameters:
tenant- the tenant being removedcomponent- the component instance to destroy
-