类 AbstractFactoryBean<T>
- java.lang.Object
- org.springframework.beans.factory.config.AbstractFactoryBean<T>
- 所有已实现的接口:
Aware
,BeanClassLoaderAware
,BeanFactoryAware
,DisposableBean
,FactoryBean<T>
,InitializingBean
- 直接已知子类:
AbstractServiceLoaderBasedFactoryBean
,ListFactoryBean
,MapFactoryBean
,ObjectFactoryCreatingFactoryBean
,ProviderCreatingFactoryBean
,SetFactoryBean
,SortedResourcesFactoryBean
public abstract class AbstractFactoryBean<T> extends Object implements FactoryBean<T>, BeanClassLoaderAware, BeanFactoryAware, InitializingBean, DisposableBean
Simple template superclass forFactoryBean
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 thegetObject()
method.Else, this class will create a new instance every time the
getObject()
method is invoked. Subclasses are responsible for implementing the abstractcreateInstance()
template method to actually create the object(s) to expose.- 从以下版本开始:
- 1.0.2
- 作者:
- Juergen Hoeller, Keith Donald
- 另请参阅:
setSingleton(boolean)
,createInstance()
构造器概要
构造器 构造器 说明 AbstractFactoryBean()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 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 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 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?void
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the beanclass 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.
构造器详细资料
AbstractFactoryBean
public AbstractFactoryBean()
方法详细资料
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()
从接口复制的说明:FactoryBean
Is 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 returntrue
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 extendedSmartFactoryBean
interface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()
method. PlainFactoryBean
implementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()
implementation returnsfalse
.- 指定者:
isSingleton
在接口中FactoryBean<T>
- 返回:
- whether the exposed object is a singleton
- 另请参阅:
FactoryBean.getObject()
,SmartFactoryBean.isPrototype()
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
从接口复制的说明:BeanClassLoaderAware
Callback that supplies the beanclass 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.- 指定者:
setBeanClassLoader
在接口中BeanClassLoaderAware
- 参数:
classLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAware
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.- 指定者:
setBeanFactory
在接口中BeanFactoryAware
- 参数:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
getBeanFactory
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.
afterPropertiesSet
public void afterPropertiesSet() throws Exception
Eagerly create the singleton instance, if necessary.- 指定者:
afterPropertiesSet
在接口中InitializingBean
- 抛出:
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 Exception
Expose the singleton instance or create a new prototype instance.- 指定者:
getObject
在接口中FactoryBean<T>
- 返回:
- an instance of the bean (can be
null
) - 抛出:
Exception
- in case of creation errors- 另请参阅:
createInstance()
,getEarlySingletonInterfaces()
destroy
public void destroy() throws Exception
Destroy the singleton instance, if any.- 指定者:
destroy
在接口中DisposableBean
- 抛出:
Exception
- in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources as well.- 另请参阅:
destroyInstance(Object)
getObjectType
public abstract Class<?> getObjectType()
This abstract method declaration mirrors the method in the FactoryBean interface, for a consistent offering of abstract template methods.- 指定者:
getObjectType
在接口中FactoryBean<T>
- 返回:
- the type of object that this FactoryBean creates, or
null
if not known at the time of the call - 另请参阅:
FactoryBean.getObjectType()
createInstance
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.- 返回:
- the object returned by this factory
- 抛出:
Exception
- if an exception occurred during object creation- 另请参阅:
getObject()
getEarlySingletonInterfaces
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.- 返回:
- the interfaces to use for 'early singletons', or
null
to indicate a FactoryBeanNotInitializedException - 另请参阅:
FactoryBeanNotInitializedException
destroyInstance
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.
- 参数:
instance
- the singleton instance, as returned bycreateInstance()
- 抛出:
Exception
- in case of shutdown errors- 另请参阅:
createInstance()