23.8 Using the ApplicationRunner or CommandLineRunner
If you need to run some specific code once the SpringApplication
has started, you can implement the ApplicationRunner
or CommandLineRunner
interfaces. Both interfaces work in the same way and offer a single run
method which will be called just before SpringApplication.run(…)
completes.
The CommandLineRunner
interfaces provides access to application arguments as a simple string array, whereas the ApplicationRunner
uses the ApplicationArguments
interface discussed above.
import org.springframework.boot.* import org.springframework.stereotype.* _@Component_ public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } }
You can additionally implement the org.springframework.core.Ordered
interface or use the org.springframework.core.annotation.Order
annotation if several CommandLineRunner
or ApplicationRunner
beans are defined that must be called in a specific order.