Centurion, South Africa info@code2bits.com 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:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

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.

@SpringBootApplication
public class MultiConsoleApplication {

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

    public static void main(String[] args) {
        LOG.info("Spring Application Start");
        SpringApplication.run(MultiConsoleApplication.class, args);
        LOG.info("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.

@Component
@Order(2)
public class CommandLineRunner1 implements CommandLineRunner {

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

    @Override
    public void run(String... args) {
        LOG.info("CommandLineRunner 1: Now Executing ");
    }
}

The CommandLineRunner2 class also implements the CommandLineRunner interface.

@Order(1) indicates it will run first.

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

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

    @Override
    public void run(String... args) {
        LOG.info("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".

Summary

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.