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:
HomologeneServiceFactory
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
ConstructorsModifierConstructorDescriptionprotected
protected
AbstractAsyncFactoryBean
(ExecutorService executor) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract T
Create a new bean as pergetObject()
.final void
destroy()
Obtain a bean asynchronously.final Class
<?> final boolean
Indicate 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, wait
Methods inherited from interface org.springframework.beans.factory.FactoryBean
isSingleton
-
Constructor Details
-
AbstractAsyncFactoryBean
protected AbstractAsyncFactoryBean() -
AbstractAsyncFactoryBean
-
-
Method Details
-
getObject
Description copied from interface:AsyncFactoryBean
Obtain a bean asynchronously.- Specified by:
getObject
in interfaceAsyncFactoryBean<T>
- Specified by:
getObject
in interfaceorg.springframework.beans.factory.FactoryBean<T>
- Returns:
- a future that completes when the bean is ready
-
getObjectType
- Specified by:
getObjectType
in 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
-
isInitialized
public final boolean isInitialized()Description copied from interface:AsyncFactoryBean
Indicate 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:
isInitialized
in interfaceAsyncFactoryBean<T>
-
destroy
public final void destroy()- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
-