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:

Giới thiệu Google Guice – Injection, Scope
Wrapper Classes in Java
The Java 8 Stream API Tutorial
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement Coppersmith Freivald’s Algorithm
A Guide to @RepeatedTest in Junit 5
Spring MVC Custom Validation
Transactions with Spring and JPA
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Creating Docker Images with Spring Boot
Java Program to Represent Graph Using Incidence List
Quick Intro to Spring Cloud Configuration
Prevent Cross-Site Scripting (XSS) in a Spring Application
Debug a HttpURLConnection problem
Java Program to Implement Variable length array
Using Spring ResponseEntity to Manipulate the HTTP Response
Simple Single Sign-On with Spring Security OAuth2
Spring MVC and the @ModelAttribute Annotation
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Guide to PriorityBlockingQueue in Java
Java Program to Implement the String Search Algorithm for Short Text Sizes
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Programmatic Transaction Management in Spring
Tạo chương trình Java đầu tiên sử dụng Eclipse
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Convert char to String in Java
Query Entities by Dates and Times with Spring Data JPA
Cơ chế Upcasting và Downcasting trong java
Java Program to Implement Gaussian Elimination Algorithm
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach