public class BeanHelper
extends java.lang.Object
A helper class for creating bean instances that are defined in configuration files.
This class provides static utility methods related to bean creation operations. These methods simplify such operations because a client need not deal with all involved interfaces. Usually, if a bean declaration has already been obtained, a single method call is necessary to create a new bean instance.
This class also supports the registration of custom bean factories.
Implementations of the
interface can be
registered under a symbolic name using the BeanFactory
registerBeanFactory()
method. In the configuration file the name of the bean factory can be
specified in the bean declaration. Then this factory will be used to create
the bean.
Modifier and Type | Field and Description |
---|---|
private static java.util.Map |
beanFactories
Stores a map with the registered bean factories.
|
private static BeanFactory |
defaultBeanFactory
Stores the default bean factory, which will be used if no other factory
is provided.
|
Modifier | Constructor and Description |
---|---|
private |
BeanHelper()
Private constructor, so no instances can be created.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
createBean(BeanDeclaration data)
Returns a bean instance for the specified declaration.
|
static java.lang.Object |
createBean(BeanDeclaration data,
java.lang.Class defaultClass)
Returns a bean instance for the specified declaration.
|
static java.lang.Object |
createBean(BeanDeclaration data,
java.lang.Class defaultClass,
java.lang.Object param)
The main method for creating and initializing beans from a configuration.
|
static BeanFactory |
deregisterBeanFactory(java.lang.String name)
Deregisters the bean factory with the given name.
|
private static java.lang.Class |
fetchBeanClass(BeanDeclaration data,
java.lang.Class defaultClass,
BeanFactory factory)
Determines the class of the bean to be created.
|
private static BeanFactory |
fetchBeanFactory(BeanDeclaration data)
Obtains the bean factory to use for creating the specified bean.
|
static BeanFactory |
getDefaultBeanFactory()
Returns the default bean factory.
|
static void |
initBean(java.lang.Object bean,
BeanDeclaration data)
Initializes the passed in bean.
|
private static void |
initProperty(java.lang.Object bean,
java.lang.String propName,
java.lang.Object value)
Sets a property on the given bean using Common Beanutils.
|
(package private) static java.lang.Class |
loadClass(java.lang.String name,
java.lang.Class callingClass)
Returns a
java.lang.Class object for the specified name. |
static void |
registerBeanFactory(java.lang.String name,
BeanFactory factory)
Register a bean factory under a symbolic name.
|
static java.util.Set |
registeredFactoryNames()
Returns a set with the names of all currently registered bean factories.
|
static void |
setDefaultBeanFactory(BeanFactory factory)
Sets the default bean factory.
|
private static java.util.Map beanFactories
private static BeanFactory defaultBeanFactory
private BeanHelper()
public static void registerBeanFactory(java.lang.String name, BeanFactory factory)
name
- the name of the factoryfactory
- the factory to be registeredpublic static BeanFactory deregisterBeanFactory(java.lang.String name)
name
- the name of the factory to be deregisteredpublic static java.util.Set registeredFactoryNames()
public static BeanFactory getDefaultBeanFactory()
public static void setDefaultBeanFactory(BeanFactory factory)
factory
- the default bean factory (must not be null)public static void initBean(java.lang.Object bean, BeanDeclaration data) throws ConfigurationRuntimeException
bean
- the bean to be initializeddata
- the bean declarationConfigurationRuntimeException
- if a property cannot be setprivate static void initProperty(java.lang.Object bean, java.lang.String propName, java.lang.Object value) throws ConfigurationRuntimeException
bean
- the beanpropName
- the name of the propertyvalue
- the property's valueConfigurationRuntimeException
- if the property is not writeable or
an error occurredpublic static java.lang.Object createBean(BeanDeclaration data, java.lang.Class defaultClass, java.lang.Object param) throws ConfigurationRuntimeException
data
- the bean declarationdefaultClass
- the default class to useparam
- an additional parameter that will be passed to the bean
factory; some factories may support parameters and behave different
depending on the value passed in hereConfigurationRuntimeException
- if an error occurspublic static java.lang.Object createBean(BeanDeclaration data, java.lang.Class defaultClass) throws ConfigurationRuntimeException
createBean(data, null, null);
.data
- the bean declarationdefaultClass
- the class to be used when in the declation no class
is specifiedConfigurationRuntimeException
- if an error occurspublic static java.lang.Object createBean(BeanDeclaration data) throws ConfigurationRuntimeException
createBean(data, null);
.data
- the bean declarationConfigurationRuntimeException
- if an error occursstatic java.lang.Class loadClass(java.lang.String name, java.lang.Class callingClass) throws java.lang.ClassNotFoundException
java.lang.Class
object for the specified name.
This method and the helper method it invokes are very similar to code
extracted from the ClassLoaderUtils
class of Commons
Jelly. It should be replaced if Commons Lang provides a generic version.name
- the name of the class to be loadedcallingClass
- the calling classjava.lang.ClassNotFoundException
- if the class cannot be loadedprivate static java.lang.Class fetchBeanClass(BeanDeclaration data, java.lang.Class defaultClass, BeanFactory factory) throws ConfigurationRuntimeException
data
- the bean declarationdefaultClass
- the default classfactory
- the bean factory to useConfigurationRuntimeException
- if the class cannot be determinedprivate static BeanFactory fetchBeanFactory(BeanDeclaration data) throws ConfigurationRuntimeException
data
- the bean declarationConfigurationRuntimeException
- if the factory cannot be determined