Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and why do we need ApplicationRunner and Runner interface?

Tags:

I'm learning Spring boot. What's some typical use cases for ApplicationRunner or any runner interface?

import org.junit.jupiter.api.Test;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class PersistencedemoApplicationTests implements ApplicationRunner {

    @Test
    void contextLoads() {
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
       // load initial data in test DB
    }
}

This is one case I'm aware of. Anything else?

like image 430
typing... Avatar asked Dec 13 '19 19:12

typing...


People also ask

What is the use of ApplicationRunner in spring boot?

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.

What is the difference between ApplicationRunner and command line runner?

Both of them provides the same functionality and the only difference between CommandLineRunner and ApplicationRunner is CommandLineRunner. run() accepts String array[] whereas ApplicationRunner. run() accepts ApplicationArguments as argument.

Why do we use CommandLineRunner?

CommandLineRunner is an interface used to indicate that a bean should run when it is contained within a SpringApplication. A Spring Boot application can have multiple beans implementing CommandLineRunner . These can be ordered with @Order .

What is command line runner interface?

CommandLineRunner is a simple Spring Boot interface with a run method. Spring Boot will automatically call the run method of all beans implementing this interface after the application context has been loaded.


1 Answers

These runners are used to run the logic on application startup, for example spring boot has ApplicationRunner(Functional Interface) with run method

ApplicationRunner run() will get execute, just after applicationcontext is created and before spring boot application startup.

ApplicationRunner takes ApplicationArgument which has convenient methods like getOptionNames(), getOptionValues() and getSourceArgs().

And CommandLineRunner is also a Functional interface with run method

CommandLineRunner run() will get execute, just after applicationcontext is created and before spring boot application starts up.

It accepts the argument, which are passed at time of server startup.

Both of them provides the same functionality and the only difference between CommandLineRunner and ApplicationRunner is CommandLineRunner.run() accepts String array[] whereas ApplicationRunner.run() accepts ApplicationArguments as argument. you can find more information with example here

like image 172
Deadpool Avatar answered Sep 18 '22 16:09

Deadpool