Udo Held
Udo Held

Reputation: 12548

Load properties for spring context from command-line

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?

Upvotes: 5

Views: 7793

Answers (1)

sundar
sundar

Reputation: 1760

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.

Upvotes: 7

Related Questions