|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.beans.factory.config.AbstractFactoryBean
public abstract class AbstractFactoryBean
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.
setSingleton(boolean)
,
createInstance()
Field Summary | |
---|---|
protected Log |
logger
Logger available to subclasses |
Constructor Summary | |
---|---|
AbstractFactoryBean()
|
Method Summary | |
---|---|
void |
afterPropertiesSet()
Eagerly create the singleton instance, if necessary. |
protected abstract Object |
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(Object 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. |
Object |
getObject()
Expose the singleton instance or create a new prototype instance. |
abstract Class |
getObjectType()
This abstract method declaration shadows the method in the FactoryBean interface. |
boolean |
isSingleton()
Is the object managed by this factory a singleton? |
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 else. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final Log logger
Constructor Detail |
---|
public AbstractFactoryBean()
Method Detail |
---|
public void setSingleton(boolean singleton)
true
(a singleton).
public boolean isSingleton()
FactoryBean
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
.
isSingleton
in interface FactoryBean
FactoryBean.getObject()
,
SmartFactoryBean.isPrototype()
public void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
protected BeanFactory getBeanFactory()
protected TypeConverter getBeanTypeConverter()
Falls back to a SimpleTypeConverter when not running in a BeanFactory.
public void afterPropertiesSet() throws Exception
afterPropertiesSet
in interface InitializingBean
Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.public final Object getObject() throws Exception
getObject
in interface FactoryBean
null
)
Exception
- in case of creation errorscreateInstance()
,
getEarlySingletonInterfaces()
public void destroy() throws Exception
destroy
in interface DisposableBean
Exception
- in case of shutdown errors.
Exceptions will get logged but not rethrown to allow
other beans to release their resources too.destroyInstance(Object)
public abstract Class getObjectType()
getEarlySingletonInterfaces
implementation
in this class work on Sun's JDK 1.3 classic VM, which can't find the method
when executing getEarlySingletonInterfaces
else.
getObjectType
in interface FactoryBean
null
if not known at the time of the callFactoryBean.getObjectType()
,
getEarlySingletonInterfaces()
protected abstract Object createInstance() throws Exception
Invoked on initialization of this FactoryBean in case of
a singleton; else, on each getObject()
call.
Exception
- if an exception occured during object creationgetObject()
protected Class[] getEarlySingletonInterfaces()
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.
null
to indicate a FactoryBeanNotInitializedExceptionFactoryBeanNotInitializedException
protected void destroyInstance(Object instance) throws Exception
The default implementation is empty.
instance
- the singleton instance, as returned by
createInstance()
Exception
- in case of shutdown errorscreateInstance()
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |