Spring Boot Console Application using CommandLineRunner

Tags: Spring Boot CommandLineRunner Order Console

In this Spring Boot tutorial, we are going to learn how to create a console application with Spring Boot using CommandLineRunner interface.

Create New Spring Boot Project

Open Spring Tool Suite IDE, select menu File > New > Spring Starter Project.

On the New Spring Starter Project popup input new project information as below and click Next.

  • Name: spring-boot-console-application
  • Group: dev.simplesolution
  • Artifact: spring-boot-console-application
  • Version: 1.0.0
  • Description: Spring Boot Console Application
  • Package: dev.simplesolution.console

Create Spring Boot Console Application Project

On the New Spring Starter Project Dependencies popup, keep it as default and click Finish to finish creating new project.

Create Spring Boot Console Application Project

You can also creating new Spring Boot project using Spring initializr online tool at start.spring.io

How to use CommandLineRunner interface

Spring Boot provides a functional interface CommandLineRunner that allows beans that implement the interface to execute the method run() when the Spring application context gets started.

For example, in the package dev.simplesolution.console we create a new class named FirstRunner and implement CommandLineRunner as below.

FirstRunner.java

package dev.simplesolution.console;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class FirstRunner implements CommandLineRunner {
	
	private Logger logger = LoggerFactory.getLogger(FirstRunner.class);

	@Override
	public void run(String... args) throws Exception {
		logger.info("Message from First Runner.");
	}

}

Run the Spring Boot application, we can see the log message from FirstRunner class as below.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2021-01-06 23:22:43.151  INFO 10244 --- [           main] d.s.c.SpringBootConsoleApplication       : Starting SpringBootConsoleApplication using Java 1.8.0_231 on simplesolution with PID 10244 (D:\SimpleSolution\spring-boot-console-application\bin\main started by SS in D:\SimpleSolution\spring-boot-console-application)
2021-01-06 23:22:43.151  INFO 10244 --- [           main] d.s.c.SpringBootConsoleApplication       : No active profile set, falling back to default profiles: default
2021-01-06 23:22:43.401  INFO 10244 --- [           main] d.s.c.SpringBootConsoleApplication       : Started SpringBootConsoleApplication in 0.447 seconds (JVM running for 1.488)
2021-01-06 23:22:43.401  INFO 10244 --- [           main] dev.simplesolution.console.FirstRunner   : Message from First Runner.

Define Multiple CommandLineRunner beans

Spring Boot allows defining multiple CommandLineRunner beans within an application context.

For example, we create a new class named SecondRunner and implement CommandLineRunner as below.

SecondRunner.java

package dev.simplesolution.console;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class SecondRunner implements CommandLineRunner {

	private Logger logger = LoggerFactory.getLogger(SecondRunner.class);
	
	@Override
	public void run(String... args) throws Exception {
		logger.info("Message from Second Runner.");
	}

}

Run the Spring Boot application, we can see the log messages from both FirstRunner and SecondRunner as below logs.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2021-01-06 23:24:53.246  INFO 19060 --- [           main] d.s.c.SpringBootConsoleApplication       : Starting SpringBootConsoleApplication using Java 1.8.0_231 on simplesolution with PID 19060 (D:\SimpleSolution\spring-boot-console-application\bin\main started by SS in D:\SimpleSolution\spring-boot-console-application)
2021-01-06 23:24:53.248  INFO 19060 --- [           main] d.s.c.SpringBootConsoleApplication       : No active profile set, falling back to default profiles: default
2021-01-06 23:24:53.501  INFO 19060 --- [           main] d.s.c.SpringBootConsoleApplication       : Started SpringBootConsoleApplication in 0.446 seconds (JVM running for 1.532)
2021-01-06 23:24:53.502  INFO 19060 --- [           main] dev.simplesolution.console.FirstRunner   : Message from First Runner.
2021-01-06 23:24:53.502  INFO 19060 --- [           main] dev.simplesolution.console.SecondRunner  : Message from Second Runner.

Using @Order() annotation

We can use @Order annotation to define the order of execution of beans that implements CommandLineRunner interface.

For example, we add @Order(1) and @Order(2) annotations to force SecondRunner to run before FirstRunner.

FirstRunner.java

package dev.simplesolution.console;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Order(2)
public class FirstRunner implements CommandLineRunner {
	
	private Logger logger = LoggerFactory.getLogger(FirstRunner.class);

	@Override
	public void run(String... args) throws Exception {
		logger.info("Message from First Runner.");
	}

}

SecondRunner.java

package dev.simplesolution.console;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

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

	private Logger logger = LoggerFactory.getLogger(SecondRunner.class);
	
	@Override
	public void run(String... args) throws Exception {
		logger.info("Message from Second Runner.");
	}

}

Run the Spring Boot application, we can see the new logs as below.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.1)

2021-01-06 23:28:38.904  INFO 19000 --- [           main] d.s.c.SpringBootConsoleApplication       : Starting SpringBootConsoleApplication using Java 1.8.0_231 on simplesolution with PID 19000 (D:\SimpleSolution\spring-boot-console-application\bin\main started by SS in D:\SimpleSolution\spring-boot-console-application)
2021-01-06 23:28:38.906  INFO 19000 --- [           main] d.s.c.SpringBootConsoleApplication       : No active profile set, falling back to default profiles: default
2021-01-06 23:28:39.162  INFO 19000 --- [           main] d.s.c.SpringBootConsoleApplication       : Started SpringBootConsoleApplication in 0.449 seconds (JVM running for 1.465)
2021-01-06 23:28:39.163  INFO 19000 --- [           main] dev.simplesolution.console.SecondRunner  : Message from Second Runner.
2021-01-06 23:28:39.163  INFO 19000 --- [           main] dev.simplesolution.console.FirstRunner   : Message from First Runner.

Final Application Source Code

At this step we have done the implementation of the Spring Boot project with structure as below.

Spring Boot Console Application Project Structure

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-console-application

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-console-application.git

or download at:

Download Source Code

Happy Coding 😊