Spring Boot - Accessing application arguments

[Updated: Apr 25, 2017, Created: Apr 25, 2017]

In Spring boot application, there are two ways we can access application arguments:

Let's see the above two ways of accessing arguments with examples.

Injecting ApplicationArguments

@SpringBootConfiguration
public class ExampleMain {
    @Bean
    MyBean myBean() {
        return new MyBean();
    }

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ExampleMain.class, args);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();
    }

    private static class MyBean {
        @Autowired
        ApplicationArguments appArgs;

        public void doSomething() {
            List<String> args = appArgs.getOptionValues("myArg");
            if (args.size() > 0) {
                System.out.printf("The value of application arg myArg: %s%n", args.get(0));
            }
        }
    }
}
mvn -q spring-boot:run -DtheMainClass="com.logicbig.example.ExampleMain" -Drun.arguments="--myArg=myArgVal"

Output

D:\example-projects\spring-boot\boot-application-argument-example>mvn -q spring-boot:run -DtheMainClass="com.logicbig.example.ExampleMain" -Drun.arguments="--myArg=myArgVal"

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)

2017-05-01 15:15:45.634 INFO 8188 --- [ main] com.logicbig.example.ExampleMain : Starting ExampleMain on JoeMsi with PID 8188 (D:\example-projects\spring-boot\boot-application-argument-example\target\classes started by Joe in D:\example-projects\spring-boot\boot-application-argument-example)
2017-05-01 15:15:45.637 INFO 8188 --- [ main] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default
2017-05-01 15:15:45.659 INFO 8188 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@286fd6e5: startup date [Mon May 01 15:15:45 CDT 2017]; root of context hierarchy
2017-05-01 15:15:45.837 INFO 8188 --- [ main] com.logicbig.example.ExampleMain : Started ExampleMain in 0.53 seconds (JVM running for 2.866)
The value of application arg myArg: myArgVal
2017-05-01 15:15:45.841 INFO 8188 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@286fd6e5: startup date [Mon May 01 15:15:45 CDT 2017]; root of context hierarchy

Note that in above command line args '-DtheMainclass' is a placeholder, which we have already defined in the configuration of Spring boot maven plugin in pom.xml (see project browser below)

By using @Value annotation

Here, we don't have to explicitly add CommandLinePropertySource to the Environment object (like we have to do with Spring core). Secondly our bean will be free from any Spring specific API.

@SpringBootConfiguration
public class ExampleMain2 {

    @Bean
    MyBean myBean() {
        return new MyBean();
    }

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ExampleMain2.class, args);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();
    }

    private static class MyBean {
        @Value("${myArg}")
        private String myArgStr;

        public void doSomething() {
            System.out.printf("The value of application arg myArg: %s%n", myArgStr);
        }
    }
}
mvn -q spring-boot:run -DtheMainClass="com.logicbig.example.ExampleMain2" -Drun.arguments="--myArg=myArgVal"

Output

D:\example-projects\spring-boot\boot-application-argument-example>mvn -q spring-boot:run -DtheMainClass="com.logicbig.example.ExampleMain2" -Drun.arguments="--myArg=myArgVal"

. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)

2017-05-01 15:15:48.445 INFO 16464 --- [ main] com.logicbig.example.ExampleMain2 : Starting ExampleMain2 on JoeMsi with PID 16464 (D:\example-projects\spring-boot\boot-application-argument-example\target\classes started by Joe in D:\example-projects\spring-boot\boot-application-argument-example)
2017-05-01 15:15:48.447 INFO 16464 --- [ main] com.logicbig.example.ExampleMain2 : No active profile set, falling back to default profiles: default
2017-05-01 15:15:48.476 INFO 16464 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3dd4d048: startup date [Mon May 01 15:15:48 CDT 2017]; root of context hierarchy
2017-05-01 15:15:48.684 INFO 16464 --- [ main] com.logicbig.example.ExampleMain2 : Started ExampleMain2 in 0.557 seconds (JVM running for 2.747)
The value of application arg myArg: myArgVal
2017-05-01 15:15:48.689 INFO 16464 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@3dd4d048: startup date [Mon May 01 15:15:48 CDT 2017]; root of context hierarchy

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter 1.5.3.RELEASE: Core starter, including auto-configuration support, logging and YAML.
    Corresponding Spring version: 4.3.8.RELEASE
  • JDK 1.8
  • Maven 3.3.9

Application Argument Examples Select All Download
  • boot-application-argument-example
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also