Amsterdam, Netherlands On Request

How to Create a Console Application using CommandLineRunner (Part 2)

In part 2 of how to creta e a console application using the CommandLineRunner, I will show you how to make use of multiple CommandLineRunner beans within the same application context. I will also show you how to control the order in which different beans execute their run methods by using the @Order annotation.

Getting Started

The following list defines the technologies and libraries I used to implement the sample code:

Multiple Posts

This will form part of a multi-part series on how to create a console application using the CommandLineRunner.

Code Example

The code example can be downloaded from Github from the button below. After you have downloaded the code onto your machine, you can either work through the guide step-by-step to understand the example, or jump to the “Compile & Run The Example” section to get the example up and running.

Download Code

Step 1: Define Maven Dependencies

The following dependencies should be included in the pom.xml file:



Step 2: Create a Spring Boot Application

The console application is implemented by a class called MultiConsoleApplication. The class contains the @SpringBootApplication annotation that enables the spring boot auto configuration mechanism, enables the component scan on the packages and allow to register extra beans in the context.

public class MultiConsoleApplication {

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

    public static void main(String[] args) {"Spring Application Start");, args);"Spring Application Complete");    


Step 3: Implement two CommandLineRunner beans

The CommandLineRunner1 class implements the CommandLineRunner interface. This Interface is used to indicate that a bean should run when it is contained within a SpringApplication. The run method will be invoked after the application context has been loaded.

The @Order annotation is used to indicate the order in which the multiple CommandLineRunner’ run() methods will be executed. @Order(2) indicates it will run second after the other CommandLineRunner bean finish.

public class CommandLineRunner1 implements CommandLineRunner {

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

    public void run(String... args) {"CommandLineRunner 1: Now Executing ");

The CommandLineRunner2 class also implements the CommandLineRunner interface.

@Order(1) indicates it will run first.

public class CommandLineRunner2 implements CommandLineRunner {

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

    public void run(String... args) {"CommandLineRunner 2: Now Executing ");

Step 3: Compile and Run the Application

Use the following command to compile the Spring Boot application making use of maven.

Build Application

$ mvn clean install

After you have successfully build the Spring Boot application, the compiled artifact can be found in the target directory. There are two ways to run the Spring Boot Application.

Run Application

$ mvn spring-boot:run
$ java -jar target/multi-0.0.1-SNAPSHOT.jar

The following display illustrate how to first build the Spring Boot application, and then start the simple console application by passing in the two arguments, "Hello" and "World".


Congratulations! You have successfully created a console application using Spring Boot that has multiple CommandLineRunner beans. I also showed you how to control the order in which different beans execute their run methods by using the @Order annotation.