How to use CommandLineRunner in Spring Boot Application?


You must be wondering what is this CommandLineRunner Interface and why it is there in Spring Boot. So in this article, I will be addressing the need for the CommandLineRunner Interface as well as how and when to use it.

GitHub Source Code - Clone Repo

Spring Boot provides two Interface (Remember its an interface)

  1. CommandLineRunner
  2. ApplicationRunner

Since this article is about the CommandLineRunner interface so we will take ApplicationRunner in another article.

Now back to the topic, since CommandLineRunner is an interface so we must implement the CommandLineRunner interface into our class.

Name : - CommandLineRunner
Type : - Interface
Overriding method : - run()

Note:- With the new release of Spring Boot v2.5 there are nothing changes in terms of application runner

Table of Content

  1. Why do we need CommandLineRunner?
  2. Lets Jump to Code
  3. When to use CommandLineRunner?
  4. Conclusion

1. Why do we need CommandLineRunner?

To answer the need of CommandLineRunner -Let’s say you have need to run Scheduled batch Job, set some system environment properties, or need to perform some DB operation just before the Spring Boot run() method is finished, so in this kind of scenario CommandLineRunner Interface comes handy. It allows you to do such an operation before the Spring Boot’s run() method finishes its execution.


2. Lets Jump to Code

As per API documentation, CommandLineRunner is an Interface, so we can not create an instance of it but we can implement it.

 1package com.jhooq.springbootcommandlinerunner;
 2
 3import org.slf4j.Logger;
 4import org.slf4j.LoggerFactory;
 5import org.springframework.boot.CommandLineRunner;
 6import org.springframework.boot.SpringApplication;
 7import org.springframework.boot.autoconfigure.SpringBootApplication;
 8
 9/**
10 * Author : Rahul Wagh
11 **/
12@SpringBootApplication
13public class SpringBootCommandLineRunner implements CommandLineRunner {
14
15    private static Logger LOG = LoggerFactory
16            .getLogger(SpringBootCommandLineRunner.class);
17
18    public static void main(String[] args) {
19        LOG.info("STARTING : Spring boot application starting");
20        SpringApplication.run(SpringBootCommandLineRunner.class, args);
21        LOG.info("STOPPED  : Spring boot application stopped");
22    }
23
24    @Override
25    public void run(String... args) throws Exception {
26        LOG.info("EXECUTING : command line runner");
27
28        for(int i=0;i<=10;i++){
29            LOG.info("Count ="+i);
30        }
31    }
32
33}

Console Output

 1[main] c.j.s.SpringBootCommandLineRunner: Starting SpringBootCommandLineRunner on Rahul-PC with PID 1776 
 2[main] c.j.s.SpringBootCommandLineRunner: No active profile set, falling back to default profiles: default
 3[main] c.j.s.SpringBootCommandLineRunner: Started SpringBootCommandLineRunner in 1.815 seconds (JVM running for 3.31)
 4[main] c.j.s.SpringBootCommandLineRunner: EXECUTING : command line runner
 5[main] c.j.s.SpringBootCommandLineRunner: Count =0
 6[main] c.j.s.SpringBootCommandLineRunner: Count =1
 7[main] c.j.s.SpringBootCommandLineRunner: Count =2
 8[main] c.j.s.SpringBootCommandLineRunner: Count =3
 9[main] c.j.s.SpringBootCommandLineRunner: Count =4
10[main] c.j.s.SpringBootCommandLineRunner: Count =5
11[main] c.j.s.SpringBootCommandLineRunner: Count =6
12[main] c.j.s.SpringBootCommandLineRunner: Count =7
13[main] c.j.s.SpringBootCommandLineRunner: Count =8
14[main] c.j.s.SpringBootCommandLineRunner: Count =9
15[main] c.j.s.SpringBootCommandLineRunner: Count =10
16[main] c.j.s.SpringBootCommandLineRunner: STOPPED  : Spring boot application stopped

3. When to use CommandLineRunner?

CommandLineRunner can be used in the following scenarios : -

  1. Need to add some additional logger information
  2. Schedule a batch job
  3. Database operation i.e. cleanup script, status update

Gotcha for option and nonoption argument

CommandLineRunner doesn’t care about the option and non-option argument, it will take all the args as an array of strings. Take a closer look at the output.

1[main] c.j.s.SpringBootCommandLineRunner: EXECUTING : command line runner
2[main] c.j.s.SpringBootCommandLineRunner: ARGS : - [--arg1=Jhooq, --arg2=CommanLineRunner]
3[main] c.j.s.SpringBootCommandLineRunner: STOPPED  : Spring boot application stopped

As you can see from the above console output, CommandLineRunner can not distinguish between the following parameters

  1. -arg1=Jhooq
  2. -arg2=CommanLineRunner All the above three arguments are treated as Array of String.

But not to worry Spring Boot provides one more interface ApplicationRunner which can be used to accomplish or identify the optional and non-optional arguments. For more details about ApplicationRunner please refer to - Application Runner


4. Conclusion

I hope this little tutorial might have helped you to get a better understanding of “How to use CommandLineRunner Interface”.
If you have any questions or queries please put your comments below and I will be happy to answer you.