public class DefaultBeanFactory extends Object implements BeanFactory
The default implementation of the BeanFactory
interface.
This class creates beans of arbitrary types using reflection. Each time the createBean()
method is invoked, a
new bean instance is created. A default bean class is not supported.
For data type conversions (which may be needed before invoking methods through reflection to ensure that the current
parameters match their declared types) a ConversionHandler
object is used. An instance of this class can be
passed to the constructor. Alternatively, a default ConversionHandler
instance is used.
An instance of this factory class will be set as the default bean factory for the BeanHelper
class. This
means that if not bean factory is specified in a BeanDeclaration
, this default instance will be used.
Modifier and Type | Field and Description |
---|---|
static DefaultBeanFactory |
INSTANCE
Stores the default instance of this class.
|
Constructor and Description |
---|
DefaultBeanFactory()
Constructs a new instance of
DefaultBeanFactory using a default ConversionHandler . |
DefaultBeanFactory(ConversionHandler convHandler)
Constructs a new instance of
DefaultBeanFactory using the specified ConversionHandler for data type
conversions. |
Modifier and Type | Method and Description |
---|---|
Object |
createBean(BeanCreationContext bcc)
Creates a new bean instance.
|
protected Object |
createBeanInstance(BeanCreationContext bcc)
Creates the bean instance.
|
protected static <T> Constructor<T> |
findMatchingConstructor(Class<T> beanClass,
BeanDeclaration data)
Evaluates constructor arguments in the specified
BeanDeclaration and tries to find a unique matching
constructor. |
ConversionHandler |
getConversionHandler()
Gets the
ConversionHandler used by this object. |
Class<?> |
getDefaultBeanClass()
Gets the default bean class used by this factory.
|
protected void |
initBeanInstance(Object bean,
BeanCreationContext bcc)
Initializes the newly created bean instance.
|
public static final DefaultBeanFactory INSTANCE
public DefaultBeanFactory()
DefaultBeanFactory
using a default ConversionHandler
.public DefaultBeanFactory(ConversionHandler convHandler)
DefaultBeanFactory
using the specified ConversionHandler
for data type
conversions.convHandler
- the ConversionHandler
; can be null, then a default handler is usedpublic ConversionHandler getConversionHandler()
ConversionHandler
used by this object.ConversionHandler
public Object createBean(BeanCreationContext bcc) throws Exception
createBeanInstance()
and
initBeanInstance()
for creating and initializing the bean. This makes it easier for derived classes that need
to change specific functionality of the base class.createBean
in interface BeanFactory
bcc
- the context object defining the bean to be createdException
- if an error occurspublic Class<?> getDefaultBeanClass()
getDefaultBeanClass
in interface BeanFactory
protected Object createBeanInstance(BeanCreationContext bcc) throws Exception
createBean()
. It uses reflection to create a new instance
of the specified class.bcc
- the context object defining the bean to be createdException
- if an error occursprotected void initBeanInstance(Object bean, BeanCreationContext bcc) throws Exception
createBean()
. It calls the
initBean()
method of the context object for performing the initialization.bean
- the newly created bean instancebcc
- the context object defining the bean to be createdException
- if an error occursprotected static <T> Constructor<T> findMatchingConstructor(Class<T> beanClass, BeanDeclaration data)
BeanDeclaration
and tries to find a unique matching
constructor. If this is not possible, an exception is thrown. Note: This method is intended to be used by concrete
BeanFactory
implementations and not by client code.T
- the type of the bean to be createdbeanClass
- the class of the bean to be createddata
- the current BeanDeclaration
ConfigurationRuntimeException
- if no single matching constructor can be foundNullPointerException
- if the bean class or bean declaration are nullCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.