CommandLinePropertySource is PropertySource subclass which is backed by command line arguments passed to a Java Application.
In this tutorial, we will see the examples of using the two CommandLinePropertySource implementations:
- SimpleCommandLinePropertySource
- JOptCommandLinePropertySource
Using SimpleCommandLinePropertySource without Spring context
package com.logicbig.example;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import java.util.Arrays;
public class CmdSourceExample1 {
public static void main(String[] args) {
SimpleCommandLinePropertySource ps = new SimpleCommandLinePropertySource(args);
Arrays.stream(ps.getPropertyNames()).forEach(s ->
System.out.printf("%s => %s%n", s, ps.getProperty(s)));
}
}
$ mvn -q compile exec:java -Dexec.mainClass="com.logicbig.example.CmdSourceExample1" -Dexec.args="--myProp=testval1 --myProp2=testVal2" myProp => testval1 myProp2 => testVal2
We can also use run configuration of an IDE to pass args. In this tutorial we are running our examples with maven.
Using SimpleCommandLinePropertySource with Spring context
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource;
@Configuration
public class CmdSourceExample2 {
@Bean
public MyBean myBean1(){
return new MyBean();
}
public static void main(String[] args) {
PropertySource theSource = new SimpleCommandLinePropertySource(args);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(CmdSourceExample2.class);
context.getEnvironment().getPropertySources().addFirst(theSource);
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
public class MyBean {
@Autowired
private Environment environment;
public void doSomething() {
String value = environment.getProperty("myProp");
System.out.println("the value of myProp: " + value);
}
}
}
$ mvn clean -q compile exec:java -Dexec.mainClass="com.logicbig.example.CmdSourceExample2" -Dexec.args="--myProp=testval1 --myProp2=testVal2" the value of myProp: testval1
Using SimpleCommandLinePropertySource with @Value annotation
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.SimpleCommandLinePropertySource;
@Configuration
public class CmdSourceExample3 {
@Bean
public MyBean myBean() {
return new MyBean();
}
public static void main(String[] args) {
PropertySource theSource = new SimpleCommandLinePropertySource(args);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().getPropertySources().addFirst(theSource);
context.register(CmdSourceExample3.class);
context.refresh();
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
public class MyBean {
@Value("${myProp}")
private String myPropValue;
public void doSomething() {
System.out.println("the value of myProp: " + myPropValue);
}
}
}
$ mvn -q compile exec:java -Dexec.mainClass="com.logicbig.example.CmdSourceExample3" -Dexec.args="--myProp=testval1 --myProp2=testVal2" the value of myProp: testval1
Using JOptCommandLinePropertySource
JOptCommandLinePropertySource is backed by OptionSet (part of a third party library, JOpt Simple, which supports various unix style options/arguments). To see what it can do, let's see a simple example without Spring first (we have already included maven dependency of JOpt simple in project browser below):
package com.logicbig.example;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
public class CmdSourceExample4 {
public static void main(String[] args) {
OptionParser parser = new OptionParser();
parser.accepts("myProp").withRequiredArg();
OptionSet options = parser.parse(args);
boolean myProp = options.hasArgument("myProp");
System.out.println(myProp);
Object myProp1 = options.valueOf("myProp");
System.out.println(myProp1);
}
}
$ mvn -q compile exec:java -Dexec.mainClass="com.logicbig.example.CmdSourceExample4" -Dexec.args="-myProp testval1" true testval1
Let's use it with JOptCommandLinePropertySource :
@Configuration
public class CmdSourceExample5 {
@Bean
public MyBean myBean() {
return new MyBean();
}
public static void main(String[] args) {
OptionParser parser = new OptionParser();
parser.accepts("myProp").withRequiredArg();
OptionSet options = parser.parse(args);
PropertySource ps = new JOptCommandLinePropertySource(options);
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
context.getEnvironment().getPropertySources().addFirst(ps);
context.register(CmdSourceExample5.class);
context.refresh();
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
public class MyBean {
@Value("${myProp}")
private String myPropValue;
public void doSomething() {
System.out.println("the value of myProp: " + myPropValue);
}
}
}
$ mvn -q compile exec:java -Dexec.mainClass="com.logicbig.example.CmdSourceExample5" -Dexec.args="-myProp testval1" the value of myProp: testval1
Example ProjectDependencies and Technologies Used: - spring-context 6.1.2 (Spring Context)
Version Compatibility: 4.3.0.RELEASE - 6.1.2 Version compatibilities of spring-context with this example: Versions in green have been tested.
- jopt-simple 5.0.4 (A Java library for parsing command line options)
- JDK 17
- Maven 3.8.1
|