Saturday 21 July 2018

Spring Boot: Access Spring application arguments

By injecting the bean 'org.springframework.boot.ApplicationArguments' in your spring application, you can access the application arguments that were passed to 'SpringApplication.run(...)' method.

Below table summarizes the methods provided by 'ApplicationArguments' interface.

Method
Description
String[] getSourceArgs()
Return the raw unprocessed arguments that were passed to the application.
Set<String> getOptionNames()
Return the names of all option arguments. For example, if the arguments were "--foo=bar --debug" would return the values ["foo", "debug"].
boolean containsOption(String name)
Return true if the arguments contain an option with the given name.
List<String> getOptionValues(String name)
Return the collection of values associated with the arguments option having the given name.

a.   if the option is present and has no argument (e.g.: "--foo"), return an empty collection ([])
b.   if the option is present and has a single value (e.g. "--foo=bar"), return a collection having one element (["bar"])
c.   if the option is present and has multiple values (e.g. "--foo=bar --foo=baz"), return a collection having elements for each value (["bar", "baz"])
d.   if the option is not present, return null
List<String> getNonOptionArgs()
Return the collection of non-option arguments parsed.

Find the below demo application.

MyBean.java

package com.sample.myApp.model;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Component;

@Component
public class MyBean {

 @Autowired
 public MyBean(ApplicationArguments args) {
  System.out.println("*******************************");
  System.out.println("Initializing my bean");
  System.out.println("OptionNames are :");
  Set<String> optionNames = args.getOptionNames();
  for (String optionName : optionNames) {
   System.out.println(optionName + " : " + args.getOptionValues(optionName));
  }

  System.out.println("Source args are:");
  String[] sourceArgs = args.getSourceArgs();
  for (String sourceArg : sourceArgs) {
   System.out.println(sourceArg);
  }
 }

}

Application.java

package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import com.sample.myApp.model.MyBean;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {

  ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Application.class, args);

  MyBean myBean = configurableApplicationContext.getBean(MyBean.class);
  
  configurableApplicationContext.close();
 }
}


Run the application and pass command line argument ‘--foo=bar –debug’ to it.

How to pass arguments in Eclipse?
Right click on ‘Application.java’.

Go to Arguments tab and add program arguments there.

Click on Run button.

You can able to see below information in the console.

*******************************
Initializing my bean
OptionNames are :
debug : []
foo : [bar]
Source args are:
--foo=bar
--debug




Previous                                                 Next                                                 Home

No comments:

Post a Comment