We have an old application which uses modules. The main entry point (main()
method) instantiates modules (classes) with reflection based on an XML configuration file, like:
<modules>
<module class="com.example.moduleone.ModuleOne" />
<module class="com.example.moduletwo.ModuleTwo" />
<modules>
A few modules have additional configuration in the modules.xml
, for example:
<modules>
<module class="com.example.modulethree.ConfigurableModule">
<config>
<keyOne>valueOne</keyOne>
<keyTwo>valueTwo</keyTwo>
</module>
<modules>
These kind of modules has a parametrized constructor which accepts an org.jdom.Element
instance (parsed from the XML file):
public ConfigurableModule(Element moduleConfig) {
...
}
We would like to use CDI/Weld (or something else) for dependency injection. How can we instantiate our modules with the DI framework?
Would it be easier with Spring or Guice?
I've figured out the answer with clay's eye opener answer. (Thank you!)
First, a bean which loads the
modules.xml
:It also produces the configuration instance for every module based on the
InjectionPoint
.The Application class, which loads the modules based on the XML configuration is the following:
And a sample module: