Announcement Announcement Module
No announcement yet.
injecting complex configuration data Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • injecting complex configuration data

    What's the best way to handle/inject complex configuration data? Lets say I have a List (Array, Hashmap...) of structured configuration data. I see the following options:

    1. Externalize the config data to XML and inject only the (class/filesystem) path to the config file:

    <property name="xmlConfPath"><value>classpath&#58;some/path/to/file.xml</value></propert>
    It requires reading XML in non-framework code by hand. Even if dom4j / jdom / castor could make this easy, it doesn't sound like a good idea to me.

    2. Make a bean-class for the structured data and inject to a list of this bean type
    <property name="confList">
        <bean class="my.ConfClass">
           <property name="id"><value>someId</value></property>
           <property name="foo"><value>1234</value></property>
           <property name="bar"><value>just an example</value></property>
        <bean class="my.ConfClass">
           <property name="id"><value>otherId</value></property>
           <property name="foo"><value>4223</value></property>
           <property name="bar"><value>another example</value></property>
    For my taste, this is much too verbose and unreadable. In addition, I like the idea more to separate some config-data from the application-context file.

    3. Implement some XML-ish PropertyPlaceholderConfigurer which is able to read XML data instead of properties can inject it into a configured bean.

    <bean name="xmlConfigurer" class="">
      <property name="location/of/config.xml"><value>classpath&#58;some/path/config.xml</value></property>
      <property name="placeholderPrefix"><value>$xml&#123;</value></property>
    <bean id="configuredBean" class="some.Class">
      <property name="configData"><value>$xml&#123;complexConfigData&#125;</value></property>
    Disadvantage: XmlPropertyPlaceholderConfigurer does not exist.

    Is there a "spring-ish" way to handle such config data? The spring-config add on seems to address this issue, but adding this and commons-config sounds like a lot of unnecessary complexity to me?

    Any ideas?


  • #2
    If your issue is to separate parts of your configuration into different files, you can do that easily.
    You can specify multiple configuration files which will get merged in your ApplicationContext. You have just to take care to use "ref bean" instead of "ref local" when referring to beans in other files.

    Besides that you can also use XML entity inclusion or (better) the Spring import tag to explicitly include other configuration fragments.

    Concerning your list of configuration beans: You could create a bean of type (say) java.util.ArrayList containing your beans, declared in a separate file and refer to this list via "ref bean" in your main configuration file.

    Hope that helps,


    • #3
      Another alternative is to just use the supplied Java platform serialization support in the java beans package, XMLEncoder and XMLDecoder.

      There was a recent article about their use. If its just data structures, then why make it dependent on external libraries (like Spring)?

      Just expressing a contrarian view.

      Of course, Spring should support these too. For example, have a factory that loads XMLEncoded beans. And support for BeanContext, sure, why not.

      J. Betancourt