Interface BeanHolder<T>

Type Parameters:
T - The type of the bean instance.
All Superinterfaces:
AutoCloseable

public interface BeanHolder<T> extends AutoCloseable
An object holding a bean instance, and allowing to release it.
  • Method Details

    • get

      T get()
      Returns:
      The bean instance. Guaranteed to always return the exact same object, i.e. beanHolder.get() == beanHolder.get() is always true.
    • close

      void close()
      Release any resource currently held by the BeanHolder.

      After this method has been called, the result of calling get() on the same instance is undefined.

      Warning: this method only releases resources that were allocated by the creator of the bean instance, and of which the bean instance itself may not be aware. If the bean instance itself (the one returned by get()) exposes any close() or other release method, they should be called before the BeanHolder is released.

      Specified by:
      close in interface AutoCloseable
      Throws:
      RuntimeException - If an error occurs while releasing resources.
    • withDependencyAutoClosing

      default BeanHolder<T> withDependencyAutoClosing(BeanHolder<?>... dependencies)
      Parameters:
      dependencies - Dependencies that should be closed eventually.
      Returns:
      A bean holder that wraps the current bean holder, and ensures the dependencies are also closed when its close() method is called.
    • of

      static <T> BeanHolder<T> of(T instance)
      Type Parameters:
      T - The type of the bean instance.
      Parameters:
      instance - The bean instance.
      Returns:
      A BeanHolder whose get() method returns the given instance, and whose close() method does not do anything.
    • ofCloseable

      static <T extends AutoCloseable> BeanHolder<T> ofCloseable(T instance)
      Type Parameters:
      T - The type of the bean instance.
      Parameters:
      instance - The bean instance.
      Returns:
      A BeanHolder whose get() method returns the given instance, and whose close() method calls AutoCloseable.close() on the given instance.
    • of

      static <T> BeanHolder<List<T>> of(List<? extends BeanHolder<? extends T>> beanHolders)
      Type Parameters:
      T - The type of the bean instances.
      Parameters:
      beanHolders - The bean holders.
      Returns:
      A BeanHolder whose get() method returns a list containing the instance of each given bean holder, in order, and whose close() method closes every given bean holder.