What is Runners : In Spring Boot

Execute any logic only once when app is started.Testing Purpose, Setup data..etc
Runners not used in Web based Apps, only console based we use genrally.
In Spring Boot Batch Processing, one pre-defined Runner exist.

Types of Runners(2)
a) CommandLineRunner (I)
b) ApplicationRunner (I)

CommandLineRunner (I)

  • This is a functional interface
  • We should define one class and override abstract method given by CommandLineRunner (I) ie run() method.
  • Must add @Component on top of runner class.So, that Spring Container detect the class and create object.
  • SpringApplication.run(__,__) logic will detect all runners and executs in order.
  • We can even define multiple runners. Executed in order.

Example

package in.example.sunil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

 

@Component
public class MessageRunner implements CommandLineRunner{

    public void run(String... args) throws Exception {

        System.out.println("FROM RUNNER!");
    }
}

ApplicationRunner (I)

 

 

Find Execution order of below Runners

Runner Name            Order
  A                     NO ORDER
  B                     @Order
  C                     @Order(10)
  D                     @Order(-9)
  E                     @Order(34)
  F                     @Order(-11)
  G                     @Order
  H                     No Order
  I                     @Order(0)