Class DefaultBeanFactory
- All Implemented Interfaces:
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.
- Since:
- 1.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DefaultBeanFactoryStores the default instance of this class. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new instance ofDefaultBeanFactoryusing a defaultConversionHandler.DefaultBeanFactory(ConversionHandler convHandler) Constructs a new instance ofDefaultBeanFactoryusing the specifiedConversionHandlerfor data type conversions. -
Method Summary
Modifier and TypeMethodDescriptionCreates a new bean instance.protected ObjectCreates the bean instance.protected static <T> Constructor<T> findMatchingConstructor(Class<T> beanClass, BeanDeclaration data) Evaluates constructor arguments in the specifiedBeanDeclarationand tries to find a unique matching constructor.Gets theConversionHandlerused by this object.Class<?> Gets the default bean class used by this factory.protected voidinitBeanInstance(Object bean, BeanCreationContext bcc) Initializes the newly created bean instance.
-
Field Details
-
INSTANCE
Stores the default instance of this class.
-
-
Constructor Details
-
DefaultBeanFactory
public DefaultBeanFactory()Constructs a new instance ofDefaultBeanFactoryusing a defaultConversionHandler. -
DefaultBeanFactory
Constructs a new instance ofDefaultBeanFactoryusing the specifiedConversionHandlerfor data type conversions.- Parameters:
convHandler- theConversionHandler; can be null, then a default handler is used- Since:
- 2.0
-
-
Method Details
-
findMatchingConstructor
protected static <T> Constructor<T> findMatchingConstructor(Class<T> beanClass, BeanDeclaration data) Evaluates constructor arguments in the specifiedBeanDeclarationand 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 concreteBeanFactoryimplementations 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 foundNullPointerException- if the bean class or bean declaration are null
-
createBean
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:
createBeanin interfaceBeanFactory- Parameters:
bcc- the context object defining the bean to be created- Returns:
- the new bean instance
- Throws:
Exception- if an error occurs
-
createBeanInstance
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:
Exception- if an error occurs
-
getConversionHandler
Gets theConversionHandlerused by this object.- Returns:
- the
ConversionHandler - Since:
- 2.0
-
getDefaultBeanClass
Gets the default bean class used by this factory. This is always null for this implementation.- Specified by:
getDefaultBeanClassin interfaceBeanFactory- Returns:
- the default bean class
-
initBeanInstance
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:
Exception- if an error occurs
-