Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two possibilities I can think of.</p> <p>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). </p> <pre><code>public class MyApp { public static String[] ARGS; public static void main(String[] args) { ARGS = args; // create context } } </code></pre> <p>You can then reference the command line arguments in Spring via:</p> <pre><code>&lt;util:constant static-field="MyApp.ARGS"/&gt; </code></pre> <p>Alternatively (if you are completely opposed to static variables), you can:</p> <p>2) Programmatically add the args to the application context:</p> <pre><code> 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" }; } </code></pre> <p>Parsing the command line arguments is left as an exercise to the reader. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload