views:

3264

answers:

5

When I'm writing a Spring command line application which parses command line arguments, how do I pass them to Spring? Would I want to have my main() structured so that it first parses the command line args and then inits Spring? Even so, how would it pass the object holding the parsed args to Spring?

+1  A: 

Hi, Here is an example to boot strap spring for a Main method, simply grab the passed params as normal then make the function you call on your bean (in the case deployer.execute()) take them as Strings or via any format you feel suitable.

public static void main(String[] args) throws IOException, ConfigurationException {
 Deployer deployer = bootstrapSpring();

 deployer.execute();
}

private static Deployer bootstrapSpring()
{
 FileSystemXmlApplicationContext appContext = new FileSystemXmlApplicationContext("spring/deployerContext.xml");

 Deployer deployer = (Deployer)appContext.getBean("deployer");
 return deployer;
}
Bradley Beddoes
Ah, I should have mentioned that my spring initialization must come after the command line has been parsed, as it depends on values from the command line
lowellk
+6  A: 

Two possibilities I can think of.

1) Set a static reference. (A static variable, although typically frowned upon, is OK in this case, because there can only be 1 command line invocation).

    public class MyApp {
      public static String[] ARGS; 
        public static void main2(String[] args) {
          ARGS = args;
          // create context
      }
    }

You can then reference the command line arguments in Spring via:

<util:constant static-field="MyApp.ARGS"/>

Alternatively (if you are completely opposed to static variables), you can:

2) Programmatically add the args to the application context:

public class MyApp2 {
public static void main(String[] args) {
 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

     // Define a bean and register it
 BeanDefinition beanDefinition = BeanDefinitionBuilder.
  rootBeanDefinition(Arrays.class, "asList")
  .addConstructorArgValue(args).getBeanDefinition();
 beanFactory.registerBeanDefinition("args", beanDefinition);
 GenericApplicationContext cmdArgCxt = new GenericApplicationContext(beanFactory);
 // Must call refresh to initialize context 
 cmdArgCxt.refresh();

 // Create application context, passing command line context as parent
 ApplicationContext mainContext = new ClassPathXmlApplicationContext(CONFIG_LOCATIONS, cmdArgCxt);

 // See if it's in the context
 System.out.println("Args: " + mainContext.getBean("args"));
}

private static String[] CONFIG_LOCATIONS = new String[] {
 "applicationContext.xml"
};

}

Parsing the command line arguments is left as an exercise to the reader.

flicken
+1 for the static reference. 6 lines of code are better than 20.
Leonel
+2  A: 

You can also pass an Object array as a second parameter to getBean which will be used as arguments to the constructor or factory.

public static void main(String[] args) {

Mybean m = (Mybean)context.getBean("mybean", new Object[] {args}); }

BeWarned
A: 

Consider the following class:

public class ExternalBeanReferneceFactoryBean 
    extends AbstractFactoryBean
    implements BeanNameAware {

    private static Map<String, Object> instances = new HashMap<String, Object>();
    private String beanName;

    /**
     * @param instance the instance to set
     */
    public static void setInstance(String beanName, Object instance) {
     instances.put(beanName, instance);
    }

    @Override
    protected Object createInstance() 
     throws Exception {
     return instances.get(beanName);
    }

    @Override
    public Class<?> getObjectType() {
     return instances.get(beanName).getClass();
    }

    @Override
    public void setBeanName(String name) {
     this.beanName = name;
    }

}

along with:

/**
 * Starts the job server.
 * @param args command line arguments
 */
public static void main(String[] args) {

 // parse the command line
 CommandLineParser parser = new GnuParser();
 CommandLine cmdLine = null;
 try {
  cmdLine = parser.parse(OPTIONS, args);
 } catch(ParseException pe) {
  System.err.println("Error parsing command line: "+pe.getMessage());
  new HelpFormatter().printHelp("command", OPTIONS);
  return;
 }

 // create root beanFactory
 DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

 // register bean definition for the command line
 ExternalBeanReferneceFactoryBean.setInstance("commandLine", cmdLine);
 beanFactory.registerBeanDefinition("commandLine", BeanDefinitionBuilder
  .rootBeanDefinition(ExternalBeanReferneceFactoryBean.class)
  .getBeanDefinition());

 // create application context
 GenericApplicationContext rootAppContext = new GenericApplicationContext(beanFactory);
 rootAppContext.refresh();

 // create the application context
 ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] { 
  "/commandlineapp/applicationContext.xml"
 }, rootAppContext);

 System.out.println(appContext.getBean("commandLine"));

}
Brian Dilley
+1  A: 

Have a look at my Spring-CLI library - at http://github.com/sazzer/spring-cli - as one way of doing this. It gives you a main class that automatically loads spring contexts and has the ability to use Commons-CLI for parsing command line arguments automatically and injecting them into your beans.

Graham