Class AbstractAsyncFactoryBean<T>
- Type Parameters:
T- type of bean that this factory provides
- All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.FactoryBean<Future<T>>,AsyncFactoryBean<T>
- Direct Known Subclasses:
OpenApiFactory
AsyncFactoryBean.
For singleton beans, this implementation holds the singleton using a Future. Thus, the createObject()
does not need to actually use a singleton pattern as it is guaranteed to be called only once when FactoryBean.isSingleton()
is true.
The implementation initializes beans using a ExecutorService which can be configured via
AbstractAsyncFactoryBean(ExecutorService). The default executor is single-threaded, which is suitable for
singleton beans.
This implementation handles destruction of the factory when the context is closed by dispatching a
Future.cancel(boolean) on any pending bean creation.
The getObject() is thread-safe and will appropriately handle multiple threads attempting to create a
singleton bean or coordinate multiple pending beans.
- Author:
- poirigui
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedprotectedAbstractAsyncFactoryBean(ExecutorService executor) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract TCreate a new bean as pergetObject().final voiddestroy()protected voiddestroyObject(T object) Destroy a singleton instance.Obtain a bean asynchronously.final Class<?> final booleanIndicate if this bean has been initialized or if its initialization is pending.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.beans.factory.FactoryBean
isSingleton
-
Constructor Details
-
AbstractAsyncFactoryBean
protected AbstractAsyncFactoryBean() -
AbstractAsyncFactoryBean
-
-
Method Details
-
getObject
Description copied from interface:AsyncFactoryBeanObtain a bean asynchronously.- Specified by:
getObjectin interfaceAsyncFactoryBean<T>- Specified by:
getObjectin interfaceorg.springframework.beans.factory.FactoryBean<T>- Returns:
- a future that completes when the bean is ready
-
getObjectType
- Specified by:
getObjectTypein interfaceorg.springframework.beans.factory.FactoryBean<T>
-
createObject
Create a new bean as pergetObject().The implementation of a singleton bean does not need to use the singleton pattern as this implementation already guarantees it.
- Throws:
Exception
-
destroyObject
Destroy a singleton instance.- Throws:
Exception
-
isInitialized
public final boolean isInitialized()Description copied from interface:AsyncFactoryBeanIndicate if this bean has been initialized or if its initialization is pending.This is only meaningful if
FactoryBean.isSingleton()is true as otherwise a new, yet to be initialized bean will be returned everytime and this method will always return false.- Specified by:
isInitializedin interfaceAsyncFactoryBean<T>
-
destroy
All pending bean creation will be cancelled as per
Future.cancel(boolean). If this is a singleton bean factory and a singleton has been created (or is in the process of),destroyObject(T)will also be called prior to cancelling it.- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean- Throws:
Exception
-