Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Spring Modules - Commons Configuration help Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Modules - Commons Configuration help

    Hello,

    I am a newby in Spring and need a few pointers. I want to use https://springmodules.dev.java.net/d...ingle/#commons, but I am not sure how to get all the parts together.

    Before I met Spring I did it like this in my singleton class:
    Code:
    XMLConfiguration config = new XMLConfiguration("my-config.xml");
    config.setReloadingStrategy(new FileChangedReloadingStrategy());
    
    String myStringProperty= config.getString("person.address");
    long myNumberProperty= config.getLong("person.yearOfBirth");
    The configuratino file (my-config.xml):
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <myRoot>
        <person>
            <address>some_string</address>
            <yearOfBirth>1999</yearOfBirth>
        </person>
    </myRoot>
    I have now modified the <bean> example from the url above to:
    Code:
    <!-- Apache Commons Configuration Composite configuration -->
        <bean id="configuration"
            class="org.springmodules.commons.configuration.CompositeConfigurationFactoryBean">
            <property name="configurations">
                <list>
                    <bean class="org.apache.commons.configuration.XMLConfiguration">
                        <constructor-arg type="java.net.URL"
                            value="classpath:my-config.xml" />  
                        <property name="reloadingStrategy"
                            ref="myCustomReloadingStrategy" />  
                    </bean>                
                </list>
            </property>
    
            <!-- define configuration as a set of spring resources -->
            <property name="locations"
                value="classpath*:META-INF/default.properties" />
        </bean>
    I have Spring and Spring modules libraries set up. Now the questinons:
    1. How to set the reloading strategy to FileChangedReloadingStrategy?
    2. How to inject the Configuration object into my classes to get the properties as in example above (myStringProperty)?

    Cheers,
    Borut

  • #2
    1. How to set the reloading strategy to FileChangedReloadingStrategy?

    Setup a reloadingStrategy bean :
    Code:
    <bean id="myCustomReloadingStrategy"  class="...FileChangedReloadingStrategy"/>
    You can also define this bean directly as property when defining the composite configuration :
    Code:
    <property name="configurations">
    <list>
    <bean class="org.apache.commons.configuration.XMLConfiguration">
    <constructor-arg type="java.net.URL"
                            value="classpath:my-config.xml" />  
    <property name="reloadingStrategy">
    <bean class="..FileChangedReloadingStrategy"/>
    </property>  
    </bean> 
    </list>
    </property>
    2. How to inject the Configuration object into my classes to get the properties as in example above (myStringProperty)?

    The CompositeConfigurationFactoryBean will return properties. If you need acces to the Configuration object, setup another bean with the "factory_prefix" :

    Code:
    <bean id="configuration" factory-bean="&amp;configurations" factory-method="getConfiguration"/>
    note the use of "&amp;" instead of "&" that is a reserved XML entity.

    You can then inject it as ref inside another bean :
    Code:
    <bean id="myService" class="">
      <property name="configuration" ref="configuration"/>
    </bean>
    Look at
    https://springmodules.dev.java.net/d...ingle/#d0e1056

    Comment


    • #3
      Hello,

      I have managed to get all the parts moving now. Thanks for the clues!

      There are at least three typos at https://springmodules.dev.java.net/d...ingle/#d0e1056

      The first bean should be named configurations (for example) as there should not be two beans with the same name.

      The second error is the factory-bean value which is wrongly set to &amp;properties. It should be the name of the first bean (&amp;configurations).

      And the third is wrong class name for the configurations bean. Instead of
      org.springmodules.commons.configuration.CompositeC onfigurationFactoryBean
      there must be
      org.springmodules.commons.configuration.CommonsCon figurationFactoryBean.
      These three can quite confuse a rookie like me.

      The solution is:

      Code:
          <!-- Apache Commons Configuration Composite configuration -->
          <bean id="configurations"
              class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
              <property name="configurations">
                  <list>
                      <bean class="org.apache.commons.configuration.XMLConfiguration">
                          <constructor-arg type="java.net.URL"
                              value="classpath:posting-config.xml" />  
                          <property name="reloadingStrategy">
                              <bean class="org.apache.commons.configuration.reloading.FileChangedReloadingStrategy"/>
                          </property>
                      </bean>                
                  </list>
              </property>
      
              <!-- define configuration as a set of spring resources -->
              <property name="locations"
                  value="classpath*:META-INF/default.properties" />
          </bean>
      
          <bean id="configuration" factory-bean="&amp;configurations" factory-method="getConfiguration"/>
      With the help of tapestry-acegi library and some more configuration I can now inject Commons Configuration into pages with

      Code:
      @InjectObject("spring:configuration")
      public abstract Configuration getConfig();
      and test

      Code:
      logger.info("person.address: " + getConfig().getString("person.address"));
      Now to new battles...

      Cheers,
      Borut

      Comment

      Working...
      X