What are the Runners classes in Spring Boot?
There are two interfaces Application Runner and Command-Line Runner interfaces in spring boot. These two interfaces are used to execute the code after the Spring Boot application is started. Spring Boot provides these interfaces to perform any actions immediately after the application has started.
What is Application Runner?
Application Runner is an interface used to execute the code after the Spring Boot application started.
package com.techieuncle.app;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExampleApplication implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication .class, args);
}
@Override
public void run(ApplicationArguments arg0) throws Exception {
System.out.println("Lets Print Hello World in Application Runner");
}
}
What is the Command Line Runner in Spring Boot?
Command Line Runner is an interface, used to execute the code after the Spring Boot application started.
How to implement the Command Line Runner interface on the main class file?
package com.techieuncle.app;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExampleApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
@Override
public void run(String... arg0) throws Exception {
System.out.println("Lets Print Hello World in Command Line Runner");
}
}