public abstract class

AbstractFactoryBean

extends Object
implements BeanClassLoaderAware BeanFactoryAware DisposableBean FactoryBean<T> InitializingBean
java.lang.Object
   ↳ org.springframework.beans.factory.config.AbstractFactoryBean<T>
Known Direct Subclasses
Known Indirect Subclasses

Class Overview

Simple template superclass for FactoryBean implementations that creates a singleton or a prototype object, depending on a flag.

If the "singleton" flag is true (the default), this class will create the object that it creates exactly once on initialization and subsequently return said singleton instance on all calls to the getObject() method.

Else, this class will create a new instance every time the getObject() method is invoked. Subclasses are responsible for implementing the abstract createInstance() template method to actually create the object(s) to expose.

Summary

Fields
protected final Log logger Logger available to subclasses
Public Constructors
AbstractFactoryBean()
Public Methods
void afterPropertiesSet()
Eagerly create the singleton instance, if necessary.
void destroy()
Destroy the singleton instance, if any.
final T getObject()
Expose the singleton instance or create a new prototype instance.
abstract 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 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.

void setBeanClassLoader(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.
Protected Methods
abstract T createInstance()
Template method that subclasses must override to construct the object returned by this factory.
void destroyInstance(T instance)
Callback for destroying a singleton instance.
BeanFactory getBeanFactory()
Return the BeanFactory that this bean runs in.
TypeConverter getBeanTypeConverter()
Obtain a bean type converter from the BeanFactory that this bean runs in.
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.
[Expand]
Inherited Methods
From class java.lang.Object
From interface org.springframework.beans.factory.BeanClassLoaderAware
From interface org.springframework.beans.factory.BeanFactoryAware
From interface org.springframework.beans.factory.DisposableBean
From interface org.springframework.beans.factory.FactoryBean
From interface org.springframework.beans.factory.InitializingBean

Fields

protected final Log logger

Also: SpringBeans

Logger available to subclasses

Public Constructors

public AbstractFactoryBean ()

Also: SpringBeans

Public Methods

public void afterPropertiesSet ()

Also: SpringBeans

Eagerly create the singleton instance, if necessary.

Throws
Exception

public void destroy ()

Also: SpringBeans

Destroy the singleton instance, if any.

Throws
Exception

public final T getObject ()

Also: SpringBeans

Expose the singleton instance or create a new prototype instance.

Returns
  • an instance of the bean (can be null)
Throws
Exception

public abstract Class<?> getObjectType ()

Also: SpringBeans

This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.

Returns
  • the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also

public boolean isSingleton ()

Also: SpringBeans

Is the object managed by this factory a singleton? That is, will 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 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.

Returns
  • whether the exposed object is a singleton

public void setBeanClassLoader (ClassLoader classLoader)

Also: SpringBeans

Callback that supplies the bean class loader to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean's afterPropertiesSet() method or a custom init-method.

Parameters
classLoader the owning class loader; may be null in which case a default ClassLoader must be used, for example the ClassLoader obtained via getDefaultClassLoader()

public void setBeanFactory (BeanFactory beanFactory)

Also: SpringBeans

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as afterPropertiesSet() or a custom init-method.

Parameters
beanFactory owning BeanFactory (never null). The bean can immediately call methods on the factory.

public void setSingleton (boolean singleton)

Also: SpringBeans

Set if a singleton should be created, or a new object on each request otherwise. Default is true (a singleton).

Protected Methods

protected abstract T createInstance ()

Also: SpringBeans

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
Exception if an exception occured during object creation
See Also

protected void destroyInstance (T instance)

Also: SpringBeans

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
Exception in case of shutdown errors
See Also

protected BeanFactory getBeanFactory ()

Also: SpringBeans

Return the BeanFactory that this bean runs in.

protected TypeConverter getBeanTypeConverter ()

Also: SpringBeans

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.

protected Class[] getEarlySingletonInterfaces ()

Also: SpringBeans

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 else. 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