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

    public abstract class AbstractAsyncFactoryBean<T>
    extends Object
    implements AsyncFactoryBean<T>, org.springframework.beans.factory.DisposableBean
    Base implementation of 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 Detail

      • AbstractAsyncFactoryBean

        protected AbstractAsyncFactoryBean()
      • AbstractAsyncFactoryBean

        protected AbstractAsyncFactoryBean​(ExecutorService executor)
    • Method Detail

      • getObject

        public final Future<T> getObject()
        Description copied from interface: AsyncFactoryBean
        Obtain a bean asynchronously.
        Specified by:
        getObject in interface AsyncFactoryBean<T>
        Specified by:
        getObject in interface org.springframework.beans.factory.FactoryBean<T>
        Returns:
        a future that completes when the bean is ready
      • getObjectType

        public final Class<?> getObjectType()
        Specified by:
        getObjectType in interface org.springframework.beans.factory.FactoryBean<T>
      • createObject

        protected abstract T createObject()
                                   throws Exception
        Create a new bean as per getObject().

        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 interface AsyncFactoryBean<T>
      • destroy

        public final void destroy()
        Specified by:
        destroy in interface org.springframework.beans.factory.DisposableBean