Saturday 21 July 2018

Spring boot: CommandLinePropertySource tutorial

CommandLinePropertySource is the abstract base class for PropertySource implementations backed by command line arguments.

JOptCommandLinePropertySource and SimpleCommandLinePropertySource classes extends CommandLinePropertySource class.



SimpleCommandLinePropertySource
This class is used to parse command line arguments passed to the application.

It can able to handle two distinct groups of command line arguments.
         a. option arguments
         b. non-option arguments

Option arguments
Option arguments follow below syntax.

Syntax
--optName[=optValue]

Option always prefixed with -– and may or may not have a value. If the option has a value associated with it, then the option name and value must be separated without spaces by an equals sign.

Example
--foo
 --foo=bar
 --foo="bar then baz"
 --foo=bar,baz,biz

Non-Option arguments
If you pass any command line argument without prefix --, then it is considered as non-option arguments.

For example, if you pass below command line arguments.
--o1=v1 --o2=v2 /path/to/file1 /path/to/file2

"o1" and "o2" would be considered as "option arguments", while the two filesystem paths qualify as "non-option arguments".

SimpleCommandLinePropertySource usage without spring context
Let’s see, how the class ‘SimpleCommandLinePropertySource’ parses the command line arguments using simple application.

Application.java

package com.sample.myApp;

import org.springframework.core.env.SimpleCommandLinePropertySource;

public class Application {

 public static void main(String args[]) {
  SimpleCommandLinePropertySource ps = new SimpleCommandLinePropertySource(args);

  String[] propertyNames = ps.getPropertyNames();
  for (String propName : propertyNames) {
   System.out.printf("propName : %s, value : %s\n", propName, ps.getProperty(propName));
  }
  
 }
}


Run the above application by passing the arguments ‘--a=10 --b=20 --c=30’. You can able to see below output in the console.

propName : a, value : 10
propName : b, value : 20
propName : c, value : 30

How to use SimpleCommandLinePropertySource with Spring context?
MyBean.java

package com.sample.myApp.model;

import java.util.List;
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("Option Arguments are :");
  Set<String> optionNames = args.getOptionNames();
  for (String optionName : optionNames) {
   System.out.println(optionName + " : " + args.getOptionValues(optionName));
  }

  System.out.println("Non Option Arguments are : ");
  List<String> nonOptionArgs = args.getNonOptionArgs();
  for (String arg : nonOptionArgs) {
   System.out.println(arg);
  }

  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 org.springframework.core.env.SimpleCommandLinePropertySource;

import com.sample.myApp.model.MyBean;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {
  SimpleCommandLinePropertySource propetySource = new SimpleCommandLinePropertySource(args);

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

  applicationContext.getEnvironment().getPropertySources().addFirst(propetySource);

  applicationContext.getBean(MyBean.class);

  applicationContext.close();
 }

}

Run ‘Application.java’, and pass command line arguments ‘--a=10 --b=20 --c=30 dev 30vms’.

you can able to see below statements in the console.
*******************************
Initializing my bean
Option Arguments are :
a : [10]
b : [20]
c : [30]
Non Option Arguments are : 
dev
30vms
Source args are:
--a=10
--b=20
--c=30
dev
30vms


Working with JOptCommandLinePropertySource
JOptCommandLinePropertySource implements CommandLinePropertySource interface, backed by a JOpt OptionSet.

To work with ‘JOptCommandLinePropertySource’ class, you should add ‘jopt-simple’ dependency to your application.

         <!-- https://mvnrepository.com/artifact/net.sf.jopt-simple/jopt-simple -->
                  <dependency>
                           <groupId>net.sf.jopt-simple</groupId>
                           <artifactId>jopt-simple</artifactId>
                           <version>5.0.4</version>
                  </dependency>


Application.java
package com.sample.myApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.JOptCommandLinePropertySource;

import com.sample.myApp.model.MyBean;

import joptsimple.OptionParser;
import joptsimple.OptionSet;

@SpringBootApplication
public class Application {

 public static void main(String args[]) {
  OptionParser parser = new OptionParser();
  parser.accepts("a");
  parser.accepts("b");
  parser.accepts("c");
  parser.accepts("dev");
  parser.accepts("30vms");

  OptionSet options = parser.parse(args);

  JOptCommandLinePropertySource propetySource = new JOptCommandLinePropertySource(options);

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

  applicationContext.getEnvironment().getPropertySources().addFirst(propetySource);

  applicationContext.getBean(MyBean.class);

  applicationContext.close();
 }

}

Run ‘Application.java’, and pass command line arguments ‘--a=10 --b=20 --c=30 dev 30vms’.


you can able to see below statements in the console.
*******************************
Initializing my bean
Option Arguments are :
a : [10]
b : [20]
c : [30]
Non Option Arguments are : 
dev
30vms
Source args are:
--a=10
--b=20
--c=30
dev
30vms





Previous                                                 Next                                                 Home

No comments:

Post a Comment