Search code examples
springstruts2startup

spring and commons configuration


I am using the commons configuration api for managing the application configuration and start up parameters.

This is a web based application with Struts 2 and Spring 3 in my project.

  • Does spring or struts have any facilities for reading configuration so I can use it instead of commons configuration
  • What is the best approach to initiate and use commons configuration bean with Spring 3 and how can i access it in my Struts 2

Please help me by configuring spring and beans with annotations !


Solution

  • Spring can read properties files (and read system en environment variables as well) no problem. For this there is a PropertyPlaceHolderConfigurer which can read properties files.

    In newer versions of Spring there is a PropertySource abstraction and Environment abstraction which can be used (there is a special placeholder-configurer which adds support for that as well). With the new PropertySource support properties can come from properties files, command line properties, environment properties, servlet context or JNDI (those are the ones supported out of the box).

    With either of these you can simply use placeholders (${...}) together with the @Value annotation (or in xml) to replace the placeholders at runtime.

    public class MyClass {
      @Value(${some.propertyname:defaultValue})
      String property
    }