CommandLineRunner Interface in Spring Boot

In this post, we will discuss and explore the CommandLineRunner interface in Spring Boot. We will be covering different features of this interface and when to use this interface.

 

Introduction

CommandLineRunner interface in Spring Boot provides an option to run a specific piece of code when the application is fully started. This interface called automatically by the Spring Boot after the initial bootstrapping of application.

 

1. CommandLineRunner

@Component
public class CustomCommandLineRunner implements CommandLineRunner {

 private static final Logger LOG = LoggerFactory.getLogger(CustomCommandLineRunner.class);

 @Override
 public void run(String...args) throws Exception {
  LOG.info("Custom command line runner is excuted with command line arguments: {}", Arrays.toString(args));
 }
}

CommandLineRunner interface offers a single run method, which is called just before SpringApplication.run(… ) completes. If we run our code, following log will be visible on the server console.

2018-07-06 21:54:11.096  INFO 27045 --- [           main] c.j.SpringBootExampleApplication         : Started SpringBootExampleApplication in 2.195 seconds (JVM running for 2.998)
2018-07-06 21:54:11.098  INFO 27045 --- [           main] c.j.commandline.CustomCommandLineRunner  : Custom command line runner is excuted with command line arguments: []

The CommandLineRunner interfaces provide access to application arguments as a simple string array.

 

2. CommandLineRunner Ordering

We can use any number of CommandLineRunner in our application. In case we like to call our CommandLineRunner in a specific order, we have the following two options.

  • Implement org.springframework.core.Ordered interface.
  • Use @Order annotation.

 

2.1 Ordering using Ordered interface

Implement Ordered interface and getOrder() method to provide priority for the custom runner.

@Component
public class CustomCommandLineRunner implements CommandLineRunner, Ordered {

 private static final Logger LOG = LoggerFactory.getLogger(CustomCommandLineRunner.class);

 @Override
 public void run(String...args) throws Exception {
  LOG.info("Custom command line runner is excuted with command line arguments: {}", Arrays.toString(args));
 }

 @Override
 public int getOrder() {
  return 2;
 }
}

 

2.2 Ordering using @Order annotation

Implement @Order annotation to provide priority for the custom runner.

@Component
@Order(1)
public class CustomCommandLineRunner2 implements CommandLineRunner {

 private static final Logger LOG = LoggerFactory.getLogger(CustomCommandLineRunner2.class);

 @Override
 public void run(String...args) throws Exception {
  LOG.info("Calling second command line runner with arguments {}", Arrays.toString(args));
 }
}

If we run our application, following output is visible on the server console

2018-07-06 22:03:13.906  INFO 27190 --- [           main] c.j.SpringBootExampleApplication         : Started SpringBootExampleApplication in 1.811 seconds (JVM running for 2.555)
2018-07-06 22:03:13.907  INFO 27190 --- [           main] c.j.c.CustomCommandLineRunner2           : Calling second command line runner with arguments []
2018-07-06 22:03:13.907  INFO 27190 --- [           main] c.j.commandline.CustomCommandLineRunner  : Custom command line runner is excuted with command line arguments: []

[pullquote align=”normal”]The lower the number, the higher the precedence [/pullquote]

 

3. When to Use CommandLineRunner

CommandLineRunner interface in Spring Boot application is an important tool. Here are some of the common use cases for this interface.

  • Preparing application initial data.
  • Source data from external services.

 

Summary

In this short post, we discussed the CommandLineRunner interface. We covered what are the use cases for the interface and how to create and order multiple interfaces based on the application requirements. Code snippets for this post are available on GitHub

Scroll to Top