5

I want to write a spring command line program that is initialized with a property file which is passed as command line parameter. How can that be done?

Starting class:

public static void main (String [] args) {
    String configFilename = args[0];
    ApplicationContext ctx = new ClassPathXmlApplicationContext(
        "classpath:/context/applicationContext.xml");
    MyBean bean = ctx.getBean(MyBean.class); 
    bean.getStarted();
}

applicationContext.xml:

<context:property-placeholder location="CONFIGFILENAME" ignore-unresolvable="true"/>

How do I get the config file name over from my main method to the actual spring context so that I can load the correct environment dependent properties?

Udo Held
  • 12,314
  • 11
  • 67
  • 93
  • 2
    You could possibly use a JVM argument instead: http://stackoverflow.com/questions/5598217/how-do-i-read-jvm-arguments-in-the-spring-applicationcontext-xml – DB5 Aug 07 '12 at 09:42

1 Answers1

7

In your case, you could better set a system property for properties file location

System.getProperties().setProperty("location", args[0]);

Then in applicationContext.xml file

<context:property-placeholder location="${location}" ignore-unresolvable="true"/>  

Hope this will solve your problem.

sundar
  • 1,760
  • 12
  • 28