org.springframework.beans.factory.config
Class AbstractFactoryBean

java.lang.Object
  extended by org.springframework.beans.factory.config.AbstractFactoryBean
All Implemented Interfaces:
DisposableBean, FactoryBean, InitializingBean
Direct Known Subclasses:
ListFactoryBean, MapFactoryBean, ObjectFactoryCreatingFactoryBean, SetFactoryBean

public abstract class AbstractFactoryBean
extends Object
implements FactoryBean, InitializingBean, DisposableBean

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.

Since:
1.0.2
Author:
Juergen Hoeller, Keith Donald
See Also:
setSingleton(boolean), createInstance()

Field Summary
protected  Log logger
          Logger available to subclasses
 
Constructor Summary
AbstractFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected abstract  Object createInstance()
          Template method that subclasses must override to construct the object returned by this factory.
 void destroy()
          Invoked by a BeanFactory on destruction of a singleton.
protected  void destroyInstance(Object instance)
          Callback for destroying a singleton instance.
 Object getObject()
          Return an instance (possibly shared or independent) of the object managed by this factory.
 boolean isSingleton()
          Is the bean managed by this factory a singleton or a prototype?
 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
 
Methods inherited from interface org.springframework.beans.factory.FactoryBean
getObjectType
 

Field Detail

logger

protected final Log logger
Logger available to subclasses

Constructor Detail

AbstractFactoryBean

public AbstractFactoryBean()
Method Detail

setSingleton

public void setSingleton(boolean singleton)
Set if a singleton should be created, or a new object on each request else. Default is "true" (a singleton).


isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? 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.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton
See Also:
FactoryBean.getObject()

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

getObject

public final Object getObject()
                       throws Exception
Description copied from interface: FactoryBean
Return an instance (possibly shared or independent) of the object managed by this factory. As with a BeanFactory, this allows support for both the Singleton and Prototype design pattern.

If this method returns null, the factory will consider the FactoryBean as not fully initialized and throw a corresponding FactoryBeanNotInitializedException.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should not be null; a null value will be considered as an indication of incomplete initialization)
Throws:
Exception - in case of creation errors
See Also:
FactoryBeanNotInitializedException

destroy

public void destroy()
             throws Exception
Description copied from interface: DisposableBean
Invoked by a BeanFactory on destruction of a singleton.

Specified by:
destroy in interface DisposableBean
Throws:
Exception - in case of shutdown errors. Exceptions will get logged but not rethrown to allow other beans to release their resources too.

createInstance

protected abstract Object 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:
the object returned by this factory
Throws:
Exception - if an exception occured during object creation
See Also:
getObject()

destroyInstance

protected void destroyInstance(Object 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 - the singleton instance, as returned by createInstance()
Throws:
Exception - in case of shutdown errors
See Also:
createInstance()


Copyright (c) 2002-2007 The Spring Framework Project.