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 Naor-Reingold Pseudo Random Function
Java Program to Implement ConcurrentSkipListMap API
Java Program to Generate N Number of Passwords of Length M Each
Java Program to Find Hamiltonian Cycle in an UnWeighted Graph
Java Program to Check Whether a Given Point is in a Given Polygon
Summing Numbers with Java Streams
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Jackson – Bidirectional Relationships
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Compute Cross Product of Two Vectors
Finding Max/Min of a List or Collection
Returning Custom Status Codes from Spring Controllers
Database Migrations with Flyway
Java Program to Implement JobStateReasons API
Intersection of Two Lists in Java
Spring Boot - Web Socket
The Spring @Controller and @RestController Annotations
Java Program to Represent Graph Using Linked List
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Spring Boot - Build Systems
Service Registration with Eureka
Java Program to Implement ArrayList API
How to Round a Number to N Decimal Places in Java
Java Program to Describe the Representation of Graph using Adjacency List
Java Program to Implement Fermat Primality Test Algorithm
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Iterating over Enum Values in Java
Convert Hex to ASCII in Java
Guide to Spring 5 WebFlux
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Java Program to Implement Radix Sort