public final class BeanHelper extends Object
A helper class for creating bean instances that are defined in configuration files.
This class provides 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 BeanFactory
interface can be registered under a symbolic name using the 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.
In order to create beans using BeanHelper
, create and instance of this class and initialize it accordingly -
a default BeanFactory
can be passed to the constructor, and additional bean factories can be registered (see
above). Then this instance can be used to create beans from BeanDeclaration
objects. BeanHelper
is
thread-safe. So an instance can be passed around in an application and shared between multiple components.
Modifier and Type | Field and Description |
---|---|
static BeanHelper |
INSTANCE
A default instance of
BeanHelper which can be shared between arbitrary components. |
Constructor and Description |
---|
BeanHelper()
Constructs a new instance of
BeanHelper with the default instance of DefaultBeanFactory as default
BeanFactory . |
BeanHelper(BeanFactory defaultBeanFactory)
Constructs a new instance of
BeanHelper and sets the specified default BeanFactory . |
Modifier and Type | Method and Description |
---|---|
static void |
copyProperties(Object dest,
Object orig)
Copies matching properties from the source bean to the destination bean using a specially configured
PropertyUtilsBean instance. |
Object |
createBean(BeanDeclaration data)
Creates a bean instance for the specified declaration.
|
Object |
createBean(BeanDeclaration data,
Class<?> defaultClass)
Creates a bean instance for the specified declaration.
|
Object |
createBean(BeanDeclaration data,
Class<?> defaultClass,
Object param)
The main method for creating and initializing beans from a configuration.
|
static org.apache.commons.beanutils.DynaBean |
createWrapDynaBean(Object bean)
Creates a
DynaBean instance which wraps the passed in bean. |
BeanFactory |
deregisterBeanFactory(String name)
Deregisters the bean factory with the given name.
|
BeanFactory |
getDefaultBeanFactory()
Gets the default bean factory.
|
void |
initBean(Object bean,
BeanDeclaration data)
Initializes the passed in bean.
|
static void |
initBeanProperties(Object bean,
BeanDeclaration data)
Initializes the beans properties.
|
void |
registerBeanFactory(String name,
BeanFactory factory)
Registers a bean factory under a symbolic name.
|
Set<String> |
registeredFactoryNames()
Gets a set with the names of all currently registered bean factories.
|
static void |
setProperty(Object bean,
String propName,
Object value)
Sets a property on the bean only if the property exists
|
public static final BeanHelper INSTANCE
BeanHelper
which can be shared between arbitrary components. If no special
configuration is needed, this instance can be used throughout an application. Otherwise, new instances can be created
with their own configuration.public BeanHelper()
BeanHelper
with the default instance of DefaultBeanFactory
as default
BeanFactory
.public BeanHelper(BeanFactory defaultBeanFactory)
BeanHelper
and sets the specified default BeanFactory
.defaultBeanFactory
- the default BeanFactory
(can be null, then a default instance is used)public void registerBeanFactory(String name, BeanFactory factory)
name
- the name of the factoryfactory
- the factory to be registeredpublic BeanFactory deregisterBeanFactory(String name)
name
- the name of the factory to be deregisteredpublic Set<String> registeredFactoryNames()
public BeanFactory getDefaultBeanFactory()
public void initBean(Object bean, BeanDeclaration data)
bean
- the bean to be initializeddata
- the bean declarationConfigurationRuntimeException
- if a property cannot be setpublic static void initBeanProperties(Object bean, BeanDeclaration data)
bean
- the bean to be initializeddata
- the bean declarationConfigurationRuntimeException
- if a property cannot be setpublic static org.apache.commons.beanutils.DynaBean createWrapDynaBean(Object bean)
DynaBean
instance which wraps the passed in bean.bean
- the bean to be wrapped (must not be null)DynaBean
wrapping the passed in beanIllegalArgumentException
- if the bean is nullpublic static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
PropertyUtilsBean
instance. This method ensures that enhanced introspection is enabled when doing the copy
operation.dest
- the destination beanorig
- the source beanNoSuchMethodException
- exception thrown by PropertyUtilsBean
InvocationTargetException
- exception thrown by PropertyUtilsBean
IllegalAccessException
- exception thrown by PropertyUtilsBean
public static void setProperty(Object bean, String propName, Object value)
bean
- the beanpropName
- the name of the propertyvalue
- the property's valueConfigurationRuntimeException
- if the property is not writable or an error occurredpublic Object createBean(BeanDeclaration data, Class<?> defaultClass, Object param)
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 Object createBean(BeanDeclaration data, Class<?> defaultClass)
createBean(data, null, null);
.data
- the bean declarationdefaultClass
- the class to be used when in the declaration no class is specifiedConfigurationRuntimeException
- if an error occurspublic Object createBean(BeanDeclaration data)
createBean(data, null);
.data
- the bean declarationConfigurationRuntimeException
- if an error occursCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.