x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Spring Boot ApplicationRunner & CommandLineRunner


    Both ApplicationRunner & CommandLineRunner provide same functionality but the only difference between CommandLineRunner and ApplicationRunner is that CommandLineRunner run() accepts String array[] whereas ApplicationRunner run() accepts ApplicationArguments as argument.

    ApplicationRunner

    ApplicationRunner is used to run the code after the Spring Boot application is started. You can use this interface to perform any actions right after the spring boot application is initialized.

    ApplicationRunner is a functional interface and it contains void run(ApplicationArguments... args) method which is invoked after the application startup.

    Multiple ApplicationRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation.


    ApplicationRunner example

    Below ApplicationRunner implementation class DemoApplicationRunner will be invoked right after application has started.

    ApplicationArguments has getOptionNames() method that returns the names of all option arguments. For example, if the arguments were --option1=value1 --option2" would return the values ["option1", "option2"].

    Also ApplicationArguments contains getNonOptionArgs() method which returns the collection of non-option arguments parsed.

    package com.example.demo;
    
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DemoApplicationRunner implements ApplicationRunner {
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("Application Runner called with " + args.getNonOptionArgs());
            System.out.println("Application Runner called with " + args.getOptionNames());
        }
    }	

    Spring Boot Application class
    package com.example.demo;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class HelloWorldApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HelloWorldApplication.class, args);
        }
    }	

    Running HelloWorldApplication

    Run this Spring Boot application using following command → java -jar demo-0.0.1-SNAPSHOT.jar --export=excel --db=oracle nonoption

    You will see the export, db options getting printed in console.

    Output :

    2020-04-06 14:25:58.923  INFO 4648 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-04-06 14:25:58.938  INFO 4648 --- [main] com.example.demo.HelloWorldApplication   : Started HelloWorldApplication in 3.761 seconds (JVM running for 5.125)
    Application Runner called with [nonoption]
    Application Runner called with [export, db]	

    CommandLineRunner

    CommandLineRunner implementation is used to run the code after the Spring Boot application is started. You can use this interface to perform any actions right after the spring boot application is initialized.

    CommandLineRunner is a functional interface and it contains void run(String... args) method which is invoked after the application startup.

    Multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the Ordered interface or @Order annotation.

    CommandLineRunner example

    Below CommandLineRunner implementation class DemoCommandLineRunner will be invoked right after application has started.

    package com.example.demo;
    
    import java.util.Arrays;
    
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DemoCommandLineRunner implements CommandLineRunner {
    
        @Override
        public void run(String... args) throws Exception {
            System.out.println("CommandLine Runner called with " + Arrays.toString(args));
        }
    }	

    2020-04-03 12:06:18.355  INFO 3764 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
    2020-04-03 12:06:18.359  INFO 3764 --- [main] com.example.demo.HelloWorldApplication   : Started HelloWorldApplication in 3.275 seconds (JVM running for 3.81)
    CommandLine Runner called with [--database=oracle]


    References :

    Spring ApplicationRunner

    Spring ApplicationArguments

    Spring CommandLineRunner

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This