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:

Java Program to Implement D-ary-Heap
Java Program to Generate All Possible Combinations Out of a, b, c, d, e
Java String Conversions
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Compute the Area of a Triangle Using Determinants
Stack Memory and Heap Space in Java
Java Program to Generate Random Numbers Using Middle Square Method
File Upload with Spring MVC
Java Program to Implement Brent Cycle Algorithm
Java Program to Implement Quick sort
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Repeated Squaring Algorithm
Java Program to Check Whether a Directed Graph Contains a Eulerian Cycle
Java Program to Implement Euclid GCD Algorithm
Display Auto-Configuration Report in Spring Boot
Remove the First Element from a List
Java Program to Represent Graph Using 2D Arrays
Spring Boot - Building RESTful Web Services
Guide to Guava Multimap
Spring Data JPA Delete and Relationships
Java Program to Implement Knapsack Algorithm
Introduction to Spring Boot CLI
Guide to Mustache with Spring Boot
Java Program to Find the Shortest Path Between Two Vertices Using Dijkstra’s Algorithm
Java Program to Implement Hash Tables with Double Hashing
Java Program to Use the Bellman-Ford Algorithm to Find the Shortest Path
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
String Joiner trong Java 8
Java Program to Find the Edge Connectivity of a Graph
Assertions in JUnit 4 and JUnit 5
Hướng dẫn sử dụng Java Generics
Base64 encoding và decoding trong Java 8