Class AbstractFactoryBean<T>
- Type Parameters:
T- the bean type
- All Implemented Interfaces:
Aware,BeanClassLoaderAware,BeanFactoryAware,DisposableBean,FactoryBean<T>,InitializingBean
- Direct Known Subclasses:
AbstractServiceLoaderBasedFactoryBean,ListFactoryBean,MapFactoryBean,ObjectFactoryCreatingFactoryBean,ProviderCreatingFactoryBean,SetFactoryBean,SortedResourcesFactoryBean
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.
- Since:
- 1.0.2
- Author:
- Juergen Hoeller, Keith Donald
- See Also:
-
Field Summary
FieldsFields inherited from interface org.springframework.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidEagerly create the singleton instance, if necessary.protected abstract TTemplate method that subclasses must override to construct the object returned by this factory.voiddestroy()Destroy the singleton instance, if any.protected voiddestroyInstance(T instance) Callback for destroying a singleton instance.protected BeanFactoryReturn the BeanFactory that this bean runs in.protected TypeConverterObtain a bean type converter from the BeanFactory that this bean runs in.protected Class<?>[]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.final TExpose the singleton instance or create a new prototype instance.abstract Class<?>This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.booleanIs the object managed by this factory a singleton? That is, willFactoryBean.getObject()always return the same object (a reference that can be cached)?voidsetBeanClassLoader(ClassLoader classLoader) Callback that supplies the beanclass loaderto a bean instance.voidsetBeanFactory(BeanFactory beanFactory) Callback that supplies the owning factory to a bean instance.voidsetSingleton(boolean singleton) Set if a singleton should be created, or a new object on each request otherwise.
-
Field Details
-
logger
Logger available to subclasses.
-
-
Constructor Details
-
AbstractFactoryBean
public AbstractFactoryBean()
-
-
Method Details
-
setSingleton
public void setSingleton(boolean singleton) Set if a singleton should be created, or a new object on each request otherwise. Default istrue(a singleton). -
isSingleton
public boolean isSingleton()Description copied from interface:FactoryBeanIs the object managed by this factory a singleton? That is, willFactoryBean.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 returntrueunless 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
falsedoes not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBeaninterface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()method. PlainFactoryBeanimplementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()implementation returnsfalse.The default implementation returns
true, since aFactoryBeantypically manages a singleton instance.- Specified by:
isSingletonin interfaceFactoryBean<T>- Returns:
- whether the exposed object is a singleton
- See Also:
-
setBeanClassLoader
Description copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
setBeanClassLoaderin interfaceBeanClassLoaderAware- Parameters:
classLoader- the owning class loader
-
setBeanFactory
Description copied from interface:BeanFactoryAwareCallback 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.- Specified by:
setBeanFactoryin interfaceBeanFactoryAware- Parameters:
beanFactory- owning BeanFactory (nevernull). The bean can immediately call methods on the factory.- See Also:
-
getBeanFactory
Return the BeanFactory that this bean runs in. -
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.
-
afterPropertiesSet
Eagerly create the singleton instance, if necessary.- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
Exception- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
-
getObject
Expose the singleton instance or create a new prototype instance.- Specified by:
getObjectin interfaceFactoryBean<T>- Returns:
- an instance of the bean (can be
null) - Throws:
Exception- in case of creation errors- See Also:
-
destroy
Destroy the singleton instance, if any.- Specified by:
destroyin interfaceDisposableBean- Throws:
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:
-
getObjectType
This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.- Specified by:
getObjectTypein interfaceFactoryBean<T>- Returns:
- the type of object that this FactoryBean creates,
or
nullif not known at the time of the call - See Also:
-
createInstance
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 occurred during object creation- See Also:
-
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
nullotherwise. 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
nullto indicate a FactoryBeanNotInitializedException - See Also:
-
destroyInstance
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 bycreateInstance()- Throws:
Exception- in case of shutdown errors- See Also:
-