Top Description Inners Fields Constructors Methods
org.springframework.beans.factory.config

public abstract Class AbstractFactoryBean<T>

extends Object
implements FactoryBean<T>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.FactoryBean
Known Direct Subclasses
org.springframework.beans.factory.config.ListFactoryBean, org.springframework.beans.factory.config.MapFactoryBean, org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean, org.springframework.beans.factory.config.ProviderCreatingFactoryBean, org.springframework.beans.factory.config.SetFactoryBean, org.springframework.beans.factory.serviceloader.AbstractServiceLoaderBasedFactoryBean
Type Parameters
<T>
the bean type
Imports
java.lang.reflect.InvocationHandler, .InvocationTargetException, .Method, .Proxy, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.SimpleTypeConverter, .TypeConverter, org.springframework.beans.factory.BeanClassLoaderAware, .BeanFactory, .BeanFactoryAware, .DisposableBean, .FactoryBean, .FactoryBeanNotInitializedException, .InitializingBean, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .ObjectUtils, .ReflectionUtils

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.

Authors
Juergen Hoeller, Keith Donald
Since
1.0.2
See Also
setSingleton, createInstance()

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
AbstractFactoryBean.EarlySingletonInvocationHandler

Reflective InvocationHandler for lazy access to the actual singleton object.

Field Summary

Modifier and TypeField and Description
private ClassLoader
private BeanFactory
private T
private boolean
protected final Log
logger

Logger available to subclasses.

private boolean
private T

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Eagerly create the singleton instance, if necessary.
protected abstract T

Returns:

the object returned by this factory
createInstance
()

Template method that subclasses must override to construct the object returned by this factory.

public void
destroy()

Implements org.springframework.beans.factory.DisposableBean.destroy.

Destroy the singleton instance, if any.
protected void
destroyInstance(T
the singleton instance, as returned by createInstance()
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. This is typically a fresh instance for each call, since TypeConverters are usually not thread-safe.

private T
getEarlySingletonInstance()

Determine an 'early singleton' instance, exposed in case of a circular reference.

protected Class<?>[]

Returns:

the interfaces to use for 'early singletons', or null to indicate a FactoryBeanNotInitializedException
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.

public final T
getObject()

Implements org.springframework.beans.factory.FactoryBean.getObject.

Expose the singleton instance or create a new prototype instance.
public abstract Class<?>
getObjectType()

Redeclares org.springframework.beans.factory.FactoryBean.getObjectType.

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

Returns:

the singleton instance that this FactoryBean holds
getSingletonInstance
()

Expose the singleton instance (for access through the 'early singleton' proxy).

public boolean
isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Is the object managed by this factory a singleton?
public void
setBeanClassLoader(ClassLoader
the owning class loader
classLoader
)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Callback that supplies the bean class loader to a bean instance.
public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setSingleton(boolean singleton)

Set if a singleton should be created, or a new object on each request otherwise.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

beanClassLoaderback to summary
private ClassLoader beanClassLoader
Annotations
@Nullable
beanFactoryback to summary
private BeanFactory beanFactory
Annotations
@Nullable
earlySingletonInstanceback to summary
private T earlySingletonInstance
Annotations
@Nullable
initializedback to summary
private boolean initialized
loggerback to summary
protected final Log logger

Logger available to subclasses.

singletonback to summary
private boolean singleton
singletonInstanceback to summary
private T singletonInstance
Annotations
@Nullable

Constructor Detail

AbstractFactoryBeanback to summary
public AbstractFactoryBean()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet() throws Exception

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Eagerly create the singleton instance, if necessary.

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason

createInstanceback to summary
protected abstract T createInstance() throws 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:T

the object returned by this factory

Exceptions
Exception:
if an exception occurred during object creation
See Also
getObject()
destroyback to summary
public void destroy() throws Exception

Implements org.springframework.beans.factory.DisposableBean.destroy.

Destroy the singleton instance, if any.

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.beans.factory.DisposableBean.destroy.

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)
destroyInstanceback to summary
protected void destroyInstance(T instance) throws Exception

Callback for destroying a singleton instance. Subclasses may override this to destroy the previously created instance.

The default implementation is empty.

Parameters
instance:T

the singleton instance, as returned by createInstance()

Exceptions
Exception:
in case of shutdown errors
See Also
createInstance()
getBeanFactoryback to summary
protected BeanFactory getBeanFactory()

Return the BeanFactory that this bean runs in.

Annotations
@Nullable
getBeanTypeConverterback to summary
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(), org.springframework.beans.SimpleTypeConverter
getEarlySingletonInstanceback to summary
private T getEarlySingletonInstance() throws Exception

Determine an 'early singleton' instance, exposed in case of a circular reference. Not called in a non-circular scenario.

Annotations
@SuppressWarnings:unchecked
getEarlySingletonInterfacesback to summary
protected 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:Class<?>[]

the interfaces to use for 'early singletons', or null to indicate a FactoryBeanNotInitializedException

Annotations
@Nullable
See Also
org.springframework.beans.factory.FactoryBeanNotInitializedException
getObjectback to summary
public final T getObject() throws Exception

Implements org.springframework.beans.factory.FactoryBean.getObject.

Expose the singleton instance or create a new prototype instance.

Returns:T

Doc from org.springframework.beans.factory.FactoryBean.getObject.

an instance of the bean (can be null)

Annotations
@Override
Exceptions
Exception:

Doc from org.springframework.beans.factory.FactoryBean.getObject.

in case of creation errors

See Also
createInstance(), getEarlySingletonInterfaces()
getObjectTypeback to summary
public abstract Class<?> getObjectType()

Redeclares org.springframework.beans.factory.FactoryBean.getObjectType.

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

Returns:Class<?>

Doc from org.springframework.beans.factory.FactoryBean.getObjectType.

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

Annotations
@Override
@Nullable
See Also
org.springframework.beans.factory.FactoryBean#getObjectType()
getSingletonInstanceback to summary
private T getSingletonInstance() throws IllegalStateException

Expose the singleton instance (for access through the 'early singleton' proxy).

Returns:T

the singleton instance that this FactoryBean holds

Annotations
@Nullable
Exceptions
IllegalStateException:
if the singleton instance is not initialized
isSingletonback to summary
public boolean isSingleton()

Overrides default org.springframework.beans.factory.FactoryBean.isSingleton.

Doc from org.springframework.beans.factory.FactoryBean.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. 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.

Returns:boolean

whether the exposed object is a singleton

Annotations
@Override
setBeanClassLoaderback to summary
public void setBeanClassLoader(ClassLoader classLoader)

Implements org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

Doc from org.springframework.beans.factory.BeanClassLoaderAware.setBeanClassLoader.

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 InitializingBean#afterPropertiesSet() method or a custom init-method.

Parameters
classLoader:ClassLoader

the owning class loader

Annotations
@Override
setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

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 InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

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

Annotations
@Override
setSingletonback to summary
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).

org.springframework.beans.factory.config back to summary

private Class AbstractFactoryBean.EarlySingletonInvocationHandler

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • org.springframework.beans.factory.config.AbstractFactoryBean.EarlySingletonInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Reflective InvocationHandler for lazy access to the actual singleton object.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(Object
the proxy instance that the method was invoked on
proxy
,
Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

EarlySingletonInvocationHandlerback to summary
private EarlySingletonInvocationHandler()

Method Detail

invokeback to summary
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.