Class DefaultBeanFactory
- java.lang.Object
-
- org.apache.commons.configuration2.beanutils.DefaultBeanFactory
-
- All Implemented Interfaces:
BeanFactory
public class DefaultBeanFactory extends java.lang.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 defaultConversionHandler
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 aBeanDeclaration
, this default instance will be used.- Since:
- 1.3
-
-
Field Summary
Fields Modifier and Type Field Description private ConversionHandler
conversionHandler
The conversion handler used by this instance.private static java.lang.String
FMT_CTOR_ERROR
A format string for generating error messages for constructor matching.static DefaultBeanFactory
INSTANCE
Stores the default instance of this class.
-
Constructor Summary
Constructors Constructor Description DefaultBeanFactory()
Constructs a new instance ofDefaultBeanFactory
using a defaultConversionHandler
.DefaultBeanFactory(ConversionHandler convHandler)
Constructs a new instance ofDefaultBeanFactory
using the specifiedConversionHandler
for data type conversions.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static <T> void
checkSingleMatchingConstructor(java.lang.Class<T> beanClass, BeanDeclaration data, java.util.List<java.lang.reflect.Constructor<T>> matchingConstructors)
Checks whether exactly one matching constructor was found.private static ConfigurationRuntimeException
constructorMatchingException(java.lang.Class<?> beanClass, BeanDeclaration data, java.lang.String msg)
Constructs an exception if no single matching constructor was found with a meaningful error message.java.lang.Object
createBean(BeanCreationContext bcc)
Creates a new bean instance.protected java.lang.Object
createBeanInstance(BeanCreationContext bcc)
Creates the bean instance.private java.lang.Object[]
fetchConstructorArgs(java.lang.reflect.Constructor<?> ctor, BeanCreationContext bcc)
Obtains the arguments for a constructor call to create a bean.protected static <T> java.lang.reflect.Constructor<T>
findMatchingConstructor(java.lang.Class<T> beanClass, BeanDeclaration data)
Evaluates constructor arguments in the specifiedBeanDeclaration
and tries to find a unique matching constructor.private static <T> java.util.List<java.lang.reflect.Constructor<T>>
findMatchingConstructors(java.lang.Class<T> beanClass, BeanDeclaration data)
Returns a list with all constructors which are compatible with the constructor arguments specified by the givenBeanDeclaration
.private static java.util.Collection<ConstructorArg>
getConstructorArgs(BeanDeclaration data)
Gets constructor arguments from a bean declaration.ConversionHandler
getConversionHandler()
Gets theConversionHandler
used by this object.java.lang.Class<?>
getDefaultBeanClass()
Gets the default bean class used by this factory.protected void
initBeanInstance(java.lang.Object bean, BeanCreationContext bcc)
Initializes the newly created bean instance.private static boolean
matchesConstructor(java.lang.reflect.Constructor<?> ctor, java.util.Collection<ConstructorArg> args)
Checks whether the given constructor is compatible with the given list of arguments.private static java.util.Collection<ConstructorArg>
nullSafeConstructorArgs(BeanDeclaration data)
Fetches constructor arguments from the given bean declaration.
-
-
-
Field Detail
-
INSTANCE
public static final DefaultBeanFactory INSTANCE
Stores the default instance of this class.
-
FMT_CTOR_ERROR
private static final java.lang.String FMT_CTOR_ERROR
A format string for generating error messages for constructor matching.- See Also:
- Constant Field Values
-
conversionHandler
private final ConversionHandler conversionHandler
The conversion handler used by this instance.
-
-
Constructor Detail
-
DefaultBeanFactory
public DefaultBeanFactory()
Constructs a new instance ofDefaultBeanFactory
using a defaultConversionHandler
.
-
DefaultBeanFactory
public DefaultBeanFactory(ConversionHandler convHandler)
Constructs a new instance ofDefaultBeanFactory
using the specifiedConversionHandler
for data type conversions.- Parameters:
convHandler
- theConversionHandler
; can be null, then a default handler is used- Since:
- 2.0
-
-
Method Detail
-
checkSingleMatchingConstructor
private static <T> void checkSingleMatchingConstructor(java.lang.Class<T> beanClass, BeanDeclaration data, java.util.List<java.lang.reflect.Constructor<T>> matchingConstructors)
Checks whether exactly one matching constructor was found. Throws a meaningful exception if there is not a single matching constructor.- Parameters:
beanClass
- the bean classdata
- the bean declarationmatchingConstructors
- the list with matching constructors- Throws:
ConfigurationRuntimeException
- if there is not exactly one match
-
constructorMatchingException
private static ConfigurationRuntimeException constructorMatchingException(java.lang.Class<?> beanClass, BeanDeclaration data, java.lang.String msg)
Constructs an exception if no single matching constructor was found with a meaningful error message.- Parameters:
beanClass
- the affected bean classdata
- the bean declarationmsg
- an error message- Returns:
- the exception with the error message
-
findMatchingConstructor
protected static <T> java.lang.reflect.Constructor<T> findMatchingConstructor(java.lang.Class<T> beanClass, BeanDeclaration data)
Evaluates constructor arguments in the specifiedBeanDeclaration
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 concreteBeanFactory
implementations and not by client code.- Type Parameters:
T
- the type of the bean to be created- Parameters:
beanClass
- the class of the bean to be createddata
- the currentBeanDeclaration
- Returns:
- the single matching constructor
- Throws:
ConfigurationRuntimeException
- if no single matching constructor can be foundjava.lang.NullPointerException
- if the bean class or bean declaration are null
-
findMatchingConstructors
private static <T> java.util.List<java.lang.reflect.Constructor<T>> findMatchingConstructors(java.lang.Class<T> beanClass, BeanDeclaration data)
Returns a list with all constructors which are compatible with the constructor arguments specified by the givenBeanDeclaration
.- Parameters:
beanClass
- the bean class to be instantiateddata
- the currentBeanDeclaration
- Returns:
- a list with all matching constructors
-
getConstructorArgs
private static java.util.Collection<ConstructorArg> getConstructorArgs(BeanDeclaration data)
Gets constructor arguments from a bean declaration. Deals with null values.- Parameters:
data
- the bean declaration- Returns:
- the collection with constructor arguments (never null)
-
matchesConstructor
private static boolean matchesConstructor(java.lang.reflect.Constructor<?> ctor, java.util.Collection<ConstructorArg> args)
Checks whether the given constructor is compatible with the given list of arguments.- Parameters:
ctor
- the constructor to be checkedargs
- the collection of constructor arguments- Returns:
- a flag whether this constructor is compatible with the given arguments
-
nullSafeConstructorArgs
private static java.util.Collection<ConstructorArg> nullSafeConstructorArgs(BeanDeclaration data)
Fetches constructor arguments from the given bean declaration. Handles null values safely.- Parameters:
data
- the bean declaration- Returns:
- the collection with constructor arguments (never null)
-
createBean
public java.lang.Object createBean(BeanCreationContext bcc) throws java.lang.Exception
Creates a new bean instance. This implementation delegates to the protected methodscreateBeanInstance()
andinitBeanInstance()
for creating and initializing the bean. This makes it easier for derived classes that need to change specific functionality of the base class.- Specified by:
createBean
in interfaceBeanFactory
- Parameters:
bcc
- the context object defining the bean to be created- Returns:
- the new bean instance
- Throws:
java.lang.Exception
- if an error occurs
-
createBeanInstance
protected java.lang.Object createBeanInstance(BeanCreationContext bcc) throws java.lang.Exception
Creates the bean instance. This method is called bycreateBean()
. It uses reflection to create a new instance of the specified class.- Parameters:
bcc
- the context object defining the bean to be created- Returns:
- the new bean instance
- Throws:
java.lang.Exception
- if an error occurs
-
fetchConstructorArgs
private java.lang.Object[] fetchConstructorArgs(java.lang.reflect.Constructor<?> ctor, BeanCreationContext bcc)
Obtains the arguments for a constructor call to create a bean. This method resolves nested bean declarations and performs necessary type conversions.- Parameters:
ctor
- the constructor to be invokedbcc
- the context object defining the bean to be created- Returns:
- an array with constructor arguments
-
getConversionHandler
public ConversionHandler getConversionHandler()
Gets theConversionHandler
used by this object.- Returns:
- the
ConversionHandler
- Since:
- 2.0
-
getDefaultBeanClass
public java.lang.Class<?> getDefaultBeanClass()
Gets the default bean class used by this factory. This is always null for this implementation.- Specified by:
getDefaultBeanClass
in interfaceBeanFactory
- Returns:
- the default bean class
-
initBeanInstance
protected void initBeanInstance(java.lang.Object bean, BeanCreationContext bcc) throws java.lang.Exception
Initializes the newly created bean instance. This method is called bycreateBean()
. It calls theinitBean()
method of the context object for performing the initialization.- Parameters:
bean
- the newly created bean instancebcc
- the context object defining the bean to be created- Throws:
java.lang.Exception
- if an error occurs
-
-