Spring

Spring Boot Runners – Application Runner and Command Line Runner

This tutorial covers the two Spring Boot Runners namely Application Runner and Command Line Runner with the help of examples.

What are Spring Boot Runners?

Spring Boot provides two runner interfaces, which are ApplicationRunner and CommandLineRunner. Both of these runners are used to execute piece of code when a Spring Boot Application starts.

Both of these interfaces are Functional Interfaces, which means they have only one functional method. In order to execute specific piece of code when Spring Boot Application starts, we need to implement either of these functional interfaces and override the single method of run.

We will have a look at both of these runners one by one.

Example of Application Runner

When a Spring Bean implements the ApplicationRunner interface in a Spring Boot application, the bean gets executed when the application is started.

ackage com.amitph.spring.dogs; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import java.util.Arrays; @Component public class ApplicationRunnerImpl implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner, printing all arguments..."); Arrays.stream(args.getSourceArgs()).forEach(System.out::println); } }
Code language: Java (java)

We have implemented the Application Runner interface into a @Component spring bean, which overrides the run method. Inside, the run method we are only just printing the Application Arguments.

We will now, package the Spring Boot app into a JAR and execute it by passing a few parameters.

java -jar dog-service-jpa.jar These are program arguments
Code language: Bash (bash)

Next is the snippet of Spring Boot application startup logs.

o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' com.amitph.spring.dogs.Application : Started Application in 4.082 seconds (JVM running for 4.511) ApplicationRunner, printing all arguments... These are program arguments
Code language: plaintext (plaintext)

The output shows that the Application Runner is executed right after the application is fully started and it also prints each of the argument on separate lines.

Example of Command Line Runner

Similar to the Application Runner, Spring Boot also provides Command Line Runner interface. The bean implementing this interface will be executed immediately after the application is fully started.

The only difference between the two runners is that the Command Line Runner receives the arguments in the form of String[], while the Application Runner receives these arguments wrapped into ApplicationArguments class.

package com.amitph.spring.dogs; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; import java.util.Arrays; @Component public class CommandLineRunnerImpl implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner, printing all arguments..."); Arrays.stream(args).forEach(System.out::println); } }
Code language: Java (java)

Next is the snippet of running the application with the same arguments.

o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' com.amitph.spring.dogs.Application : Started Application in 4.082 seconds (JVM running for 4.511) CommandLineRunner, printing all arguments... These are program arguments
Code language: plaintext (plaintext)

From the snippet, it is clear that command line runner is also executed right after the application is started.

Spring Boot Runners Execution Order

We can have multiple beans implementing both Application Runner or Command Line Runners. In such cases their run methods are executed one after the other.

However, we can use @Ordered or @Order annotations on the beans to have them executed in a specific order. We will see this with an example.

We will mark the Command Line Runner with an execution order of 1.

@Order(1) @Component public class CommandLineRunnerImpl implements CommandLineRunner {
Code language: Java (java)

Similarly, we will mark the Application Runner with an execution order of 2.

@Order(2) @Component public class ApplicationRunnerImpl implements ApplicationRunner {
Code language: Java (java)

Now, we will package the application as a JAR and execute it with the same arguments.

com.amitph.spring.dogs.Application : Started Application in 3.887 seconds (JVM running for 4.309) CommandLineRunner, printing all arguments... These are program arguments ApplicationRunner, printing all arguments... These are program arguments
Code language: plaintext (plaintext)

The output snippet indicates that both of the beans are now executed in the specified order.

When to use Spring Boot Runners?

As stated already, the Spring Boot runners are useful when we want to execute a piece of code as soon as an application is started. Although, it sounds very simple the runner implementations can be very useful. Importantly, they are executed only after the Spring Context is ready which means the runner implementation can leverage the auto-wiring and Spring Boot’s auto configured beans. Next a few examples when they can be used.

Setting up Application Level Constants

Many a times, Applications do need to maintain constants or static variables which remain unchanged throughout the application life cycle. Typically, such application level constants are read from external entities like databases, file system, or from a third party data provider service. The runner implementations are the best place interact with external entities and keep the data into application level constants.

Creating Initial Data

Runners are the best place to prepare any initial data that your application may need. Hence, using them the application can ensure that the database or other entities are in desired state.

Creating Non-Web Console Application

Another use of the Runner implementation is to create Spring Boot backed console based non-web application.

Summary

In this tutorial we covered Spring Boot Runners with examples.

  • Spring Boot provides two runner interfaces namely ApplicationRunner and CommandLineRunner.
  • Both of these interfaces are Functional Interfaces with run method. The bean implementing these interfaces are executed once the application is started.
  • The only difference between these two interfaces is the arguments they receive.
  • There can be more than once implementations of these interfaces and their order of execution can be controlled using @Ordered or @Order annotations.

For full source code of the examples used here, please visit Github.