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?

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.