Issue
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?
Solution
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
Answered By - Deadpool
Answer Checked By - Robin (JavaFixing Admin)