Results 1 to 3 of 3

Thread: injecting complex configuration data

  1. #1
    Join Date
    Dec 2004
    Location
    Germany
    Posts
    24

    Default 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:

    Code:
    <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
    Code:
    <property name="confList">
      <list>
        <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>
        <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>
         </bean>
      </list>
    </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.

    Code:
    <bean name="xmlConfigurer" class="to.be.done.XmlPropertyPlaceholderConfigurer">
      <property name="location/of/config.xml"><value>classpath&#58;some/path/config.xml</value></property>
      <property name="placeholderPrefix"><value>$xml&#123;</value></property>
    </bean>
    
    <bean id="configuredBean" class="some.Class">
      <property name="configData"><value>$xml&#123;complexConfigData&#125;</value></property>
    </bean>
    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?

    Thanks,
    Felix

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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,
    Andreas

  3. #3
    Join Date
    Aug 2004
    Location
    u.s.a
    Posts
    399

    Default

    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

Similar Threads

  1. Dynamic Property Configuration
    By fenrick in forum Container
    Replies: 3
    Last Post: May 12th, 2006, 02:38 AM
  2. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  3. Replies: 0
    Last Post: Jun 21st, 2005, 06:17 AM
  4. Where should one put configuration data?
    By dcioccar in forum Architecture
    Replies: 1
    Last Post: May 19th, 2005, 07:18 PM
  5. Replies: 5
    Last Post: Aug 27th, 2004, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •