org.springframework.beans.factory.config

Class AbstractFactoryBean<T>

  • java.lang.Object
    • org.springframework.beans.factory.config.AbstractFactoryBean<T>
    • Field Summary

      Fields 
      Modifier and Type Field and Description
      protected Log logger
      Logger available to subclasses.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method and Description
      void afterPropertiesSet()
      Eagerly create the singleton instance, if necessary.
      protected abstract T createInstance()
      Template method that subclasses must override to construct the object returned by this factory.
      void destroy()
      Destroy the singleton instance, if any.
      protected void destroyInstance(T instance)
      Callback for destroying a singleton instance.
      protected BeanFactory getBeanFactory()
      Return the BeanFactory that this bean runs in.
      protected TypeConverter getBeanTypeConverter()
      Obtain a bean type converter from the BeanFactory that this bean runs in.
      protected java.lang.Class<?>[] getEarlySingletonInterfaces()
      Return an array of interfaces that a singleton object exposed by this FactoryBean is supposed to implement, for use with an 'early singleton proxy' that will be exposed in case of a circular reference.
      T getObject()
      Expose the singleton instance or create a new prototype instance.
      abstract java.lang.Class<?> getObjectType()
      This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.
      boolean isSingleton()
      Is the object managed by this factory a singleton? That is, will FactoryBean.getObject() always return the same object (a reference that can be cached)?
      void setBeanClassLoader(java.lang.ClassLoader classLoader)
      Callback that supplies the bean class loader to a bean instance.
      void setBeanFactory(BeanFactory beanFactory)
      Callback that supplies the owning factory to a bean instance.
      void setSingleton(boolean singleton)
      Set if a singleton should be created, or a new object on each request otherwise.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • logger

        protected final Log logger
        Logger available to subclasses.
    • Constructor Detail

      • AbstractFactoryBean

        public AbstractFactoryBean()
    • Method Detail

      • setSingleton

        public void setSingleton(boolean singleton)
        Set if a singleton should be created, or a new object on each request otherwise. Default is true (a singleton).
      • isSingleton

        public boolean isSingleton()
        Description copied from interface: FactoryBean
        Is the object managed by this factory a singleton? That is, will FactoryBean.getObject() always return the same object (a reference that can be cached)?

        NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from getObject() might get cached by the owning BeanFactory. Hence, do not return true unless the FactoryBean always exposes the same reference.

        The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

        NOTE: This method returning false does not necessarily indicate that returned objects are independent instances. An implementation of the extended SmartFactoryBean interface may explicitly indicate independent instances through its SmartFactoryBean.isPrototype() method. Plain FactoryBean implementations which do not implement this extended interface are simply assumed to always return independent instances if the isSingleton() implementation returns false.

        The default implementation returns true, since a FactoryBean typically manages a singleton instance.

        Specified by:
        isSingleton in interface FactoryBean<T>
        Returns:
        whether the exposed object is a singleton
        See Also:
        FactoryBean.getObject(), SmartFactoryBean.isPrototype()
      • getBeanFactory

        @Nullable
        protected BeanFactory getBeanFactory()
        Return the BeanFactory that this bean runs in.
      • getBeanTypeConverter

        protected TypeConverter getBeanTypeConverter()
        Obtain a bean type converter from the BeanFactory that this bean runs in. This is typically a fresh instance for each call, since TypeConverters are usually not thread-safe.

        Falls back to a SimpleTypeConverter when not running in a BeanFactory.

        See Also:
        ConfigurableBeanFactory.getTypeConverter(), SimpleTypeConverter
      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Eagerly create the singleton instance, if necessary.
        Specified by:
        afterPropertiesSet in interface InitializingBean
        Throws:
        java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
      • getObject

        public final T getObject()
                          throws java.lang.Exception
        Expose the singleton instance or create a new prototype instance.
        Specified by:
        getObject in interface FactoryBean<T>
        Returns:
        an instance of the bean (can be null)
        Throws:
        java.lang.Exception - in case of creation errors
        See Also:
        createInstance(), getEarlySingletonInterfaces()
      • destroy

        public void destroy()
                     throws java.lang.Exception
        Destroy the singleton instance, if any.
        Specified by:
        destroy in interface DisposableBean
        Throws:
        java.lang.Exception - in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.
        See Also:
        destroyInstance(Object)
      • getObjectType

        @Nullable
        public abstract java.lang.Class<?> getObjectType()
        This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.
        Specified by:
        getObjectType in interface FactoryBean<T>
        Returns:
        the type of object that this FactoryBean creates, or null if not known at the time of the call
        See Also:
        FactoryBean.getObjectType()
      • createInstance

        protected abstract T createInstance()
                                     throws java.lang.Exception
        Template method that subclasses must override to construct the object returned by this factory.

        Invoked on initialization of this FactoryBean in case of a singleton; else, on each getObject() call.

        Returns:
        the object returned by this factory
        Throws:
        java.lang.Exception - if an exception occurred during object creation
        See Also:
        getObject()
      • getEarlySingletonInterfaces

        @Nullable
        protected java.lang.Class<?>[] getEarlySingletonInterfaces()
        Return an array of interfaces that a singleton object exposed by this FactoryBean is supposed to implement, for use with an 'early singleton proxy' that will be exposed in case of a circular reference.

        The default implementation returns this FactoryBean's object type, provided that it is an interface, or null otherwise. The latter indicates that early singleton access is not supported by this FactoryBean. This will lead to a FactoryBeanNotInitializedException getting thrown.

        Returns:
        the interfaces to use for 'early singletons', or null to indicate a FactoryBeanNotInitializedException
        See Also:
        FactoryBeanNotInitializedException
      • destroyInstance

        protected void destroyInstance(@Nullable
                                       T instance)
                                throws java.lang.Exception
        Callback for destroying a singleton instance. Subclasses may override this to destroy the previously created instance.

        The default implementation is empty.

        Parameters:
        instance - the singleton instance, as returned by createInstance()
        Throws:
        java.lang.Exception - in case of shutdown errors
        See Also:
        createInstance()