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.

@FunctionalInterface public interface TenantComponentFactory<T>
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 Type
    Method
    Description
    Creates a component instance for the given tenant.
    default void
    destroy(TenantDescriptor tenant, T component)
    Destroys the given component when its tenant is removed.
  • Method Details

    • create

      T create(TenantDescriptor tenant)
      Creates a component instance for the given tenant.
      Parameters:
      tenant - the tenant to create the component instance for
      Returns:
      a new component instance for the given tenant
    • destroy

      default void destroy(TenantDescriptor tenant, T component)
      Destroys the given component when its tenant is 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 removed
      component - the component instance to destroy