Spring Boot – Runners

Application Runner and Command Line Runner interfaces lets you to execute the code after the Spring Boot application is started. You can use these interfaces to perform any actions immediately after the application has started. This chapter talks about them in detail.

1. Application Runner

Application Runner is an interface used to execute the code after the Spring Boot application started. The example given below shows how to implement the Application Runner interface on the main class file.

package com.maixuanviet.demo;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements ApplicationRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Override
   public void run(ApplicationArguments arg0) throws Exception {
      System.out.println("Hello World from Application Runner");
   }
}

Now, if you observe the console window below Hello World from Application Runner, the println statement is executed after the Tomcat started. Is the following screenshot relevant?

Hello World From Application Runner

2. Command Line Runner

Command Line Runner is an interface. It is used to execute the code after the Spring Boot application started. The example given below shows how to implement the Command Line Runner interface on the main class file.

package com.maixuanviet.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
   @Override
   public void run(String... arg0) throws Exception {
      System.out.println("Hello world from Command Line Runner");
   }
}

Look at the console window below “Hello world from Command Line Runner” println statement is executed after the Tomcat started.

Command Line Runner

Related posts:

Introduction to Spliterator in Java
Java Program to Implement Miller Rabin Primality Test Algorithm
The Dining Philosophers Problem in Java
Custom Thread Pools In Java 8 Parallel Streams
Apache Commons Collections BidiMap
OAuth2 Remember Me with Refresh Token
Java Program to Check whether Undirected Graph is Connected using BFS
Java Program to Permute All Letters of an Input String
Inheritance with Jackson
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Collect a Java Stream to an Immutable Collection
Java Program to Find Location of a Point Placed in Three Dimensions Using K-D Trees
Dynamic Proxies in Java
Hướng dẫn Java Design Pattern – Strategy
Batch Processing with Spring Cloud Data Flow
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
Check If a File or Directory Exists in Java
How to Kill a Java Thread
Java Program to Perform Finite State Automaton based Search
Java Program to Represent Linear Equations in Matrix Form
Java Program to Convert a Decimal Number to Binary Number using Stacks
Hướng dẫn Java Design Pattern – Factory Method
Java Program to Perform Naive String Matching
Java Program to Implement Regular Falsi Algorithm
Hướng dẫn Java Design Pattern – Transfer Object
Most commonly used String methods in Java
Java NIO2 Path API
Java Program to Find All Pairs Shortest Path
Java Program to Represent Graph Using Incidence List
Sử dụng CyclicBarrier trong Java
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
CharSequence vs. String in Java