当我编写一个解析命令行参数的Spring命令行应用程序时,如何将它们传递给Spring?我想让我的main()结构化,以便它首先解析命令行args然后进入Spring吗?即便如此,它如何将持有解析的args的对象传递给Spring?

有帮助吗?

解决方案

我能想到的两种可能性。

1)设置静态参考。 (静态变量,虽然通常不赞成,但在这种情况下是可以的,因为只能有1个命令行调用)。

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

然后,您可以通过以下方式引用Spring中的命令行参数:

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

或者(如果您完全反对静态变量),您可以:

2)以编程方式将args添加到应用程序上下文中:

 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"
   };

 }

解析命令行参数留给读者练习。

其他提示

查看我的Spring-CLI库 - 访问 http://github.com/sazzer/spring -cli - 作为这样做的一种方式。它为您提供了一个自动加载spring上下文的主类,并且能够使用Commons-CLI自动解析命令行参数并将它们注入到bean中。

您还可以将Object数组作为第二个参数传递给 getBean ,它将用作构造函数或工厂的参数。

public static void main(String[] args) {
   Mybean m = (Mybean)context.getBean("mybean", new Object[] {args});
}

从Spring 3.1开始,不需要在其他答案中建议的任何自定义代码。查看 CommandLinePropertySource ,它提供了一种将CL参数注入上下文的自然方法。

如果你是一个幸运的Spring Boot开发人员,你可以利用 SpringApplication 为您提供以下内容:

  

默认情况下,class将执行以下步骤来引导您的   应用程序:

     

...

     

注册CommandLinePropertySource以公开命令行参数   作为Spring属性

如果您对Spring Boot属性解析顺序感兴趣,请参阅此页

考虑以下课程:

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;
    }

}

以及:

/**
 * 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"));

}

这是一个为Main方法启动弹簧弹簧的示例,只需正常抓取传递的参数,然后使你在bean上调用的函数(在deployer.execute()的情况下)将它们作为字符串或任何格式你觉得合适。

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top