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:

Spring Boot - Google Cloud Platform
Deploy a Spring Boot App to Azure
Java Program to Implement Network Flow Problem
Java Program to Generate Date Between Given Range
Generating Random Numbers in a Range in Java
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Implement Max Heap
A Guide to the Java LinkedList
Pagination and Sorting using Spring Data JPA
Spring RestTemplate Error Handling
Phương thức tham chiếu trong Java 8 – Method References
Java Program to Perform Quick Sort on Large Number of Elements
Properties with Spring and Spring Boot
Sorting Query Results with Spring Data
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Perform Naive String Matching
So sánh HashMap và HashSet trong Java
HttpClient Connection Management
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Hash Tables Chaining with Binary Trees
Giới thiệu luồng vào ra (I/O) trong Java
Hướng dẫn Java Design Pattern – Memento
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Logout in an OAuth Secured Application
Java Program to Perform Deletion in a BST
Java Program to Implement Fermat Factorization Algorithm
Chuyển đổi Array sang ArrayList và ngược lại
A Guide to EnumMap
Spring Boot Integration Testing with Embedded MongoDB
Spring Boot with Multiple SQL Import Files
Calling Stored Procedures from Spring Data JPA Repositories