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 Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement SimpeBindings API
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Jackson – Change Name of Field
Java Program to Find Path Between Two Nodes in a Graph
Send email with JavaMail
Build a REST API with Spring and Java Config
Mix plain text and HTML content in a mail
Java Program to Implement AttributeList API
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Java Program to Perform the Sorting Using Counting Sort
Get the workstation name or IP
Handling URL Encoded Form Data in Spring REST
HttpClient 4 Cookbook
An Intro to Spring Cloud Security
Quick Guide on Loading Initial Data with Spring Boot
Using JWT with Spring Security OAuth
Intro to the Jackson ObjectMapper
Java Program to Find Strongly Connected Components in Graphs
Java Program to Implement Variable length array
Java Byte Array to InputStream
Spring Boot Actuator
Java Program to Implement Gabow Algorithm
Java Program to Implement Sorted Singly Linked List
Java Program to Construct an Expression Tree for an Infix Expression
So sánh ArrayList và Vector trong Java
Java Program to Implement Graph Structured Stack
Redirect to Different Pages after Login with Spring Security
Java Program to Implement the One Time Pad Algorithm
Debug a JavaMail Program