Details
-
Improvement
-
Status: Resolved
-
Minor
-
Resolution: Fixed
-
None
-
None
-
None
-
None
-
Operating System: All
Platform: All
-
34353
Description
A useful enhancement for the configuration API could be a way to create and
initialize objects whose class and initialization parameters are defined in a
configuration file.
The idea is that both the class name and initialization parameters for an object
are defined in a standard format. A createObject(String key) method (which might
fit best in DataConfiguration?) would extract the class name from the specified
property, create the object using reflection and then populate properties via
[beanutils].
For the concrete format for declaring objects in a configuration file there are
many possibilities. We have to define a format which is easy to use, but
although suports complex initialization parameters. A possible object
declaration could look as follows (here in XML flavour, but other configuration
sources are of course possible, too):
<myobject>
<class>package.ClassName</class>
<args>
<arg1>value1</arg1>
<arg2>value2></arg2>
...
</args>
</myobject>
We could even go a step further and define an interface
public interface Configurable
{
void initialize(Configuration config);
}
If the newly created object implements this interface, its initialize() method
will be called and a sub configuration will be passed that contains the content
of the <args> section. This would allow for arbitrary complex and also
hierarchical initialization parameters. It's a bit similar to what IoC
frameworks do.
Side note: If we add more features to the configuration definition files
processed by ConfigurationFactory (e.g locators or declarations for reloading
strategies) and use a XMLConfiguration to load these files, the proposed object
creation facility may come in handy.