Interface BeanHolder<T>
- Type Parameters:
T
- The type of the bean instance.
- All Superinterfaces:
AutoCloseable
An object holding a bean instance, and allowing to release it.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Release any resource currently held by theBeanHolder
.get()
static <T> BeanHolder<List<T>>
of
(List<? extends BeanHolder<? extends T>> beanHolders) static <T> BeanHolder<T>
of
(T instance) static <T extends AutoCloseable>
BeanHolder<T>ofCloseable
(T instance) default BeanHolder<T>
withDependencyAutoClosing
(BeanHolder<?>... dependencies)
-
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 theBeanHolder
.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 anyclose()
or other release method, they should be called before theBeanHolder
is released.- Specified by:
close
in interfaceAutoCloseable
- Throws:
RuntimeException
- If an error occurs while releasing resources.
-
withDependencyAutoClosing
- 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
- Type Parameters:
T
- The type of the bean instance.- Parameters:
instance
- The bean instance.- Returns:
- A
BeanHolder
whoseget()
method returns the given instance, and whoseclose()
method does not do anything.
-
ofCloseable
- Type Parameters:
T
- The type of the bean instance.- Parameters:
instance
- The bean instance.- Returns:
- A
BeanHolder
whoseget()
method returns the given instance, and whoseclose()
method callsAutoCloseable.close()
on the given instance.
-
of
- Type Parameters:
T
- The type of the bean instances.- Parameters:
beanHolders
- The bean holders.- Returns:
- A
BeanHolder
whoseget()
method returns a list containing the instance of each given bean holder, in order, and whoseclose()
method closes every given bean holder.
-