在開發(fā)的過程中裹粤,有時(shí)候需要在應(yīng)用啟動(dòng)后去自動(dòng)進(jìn)行一些操作颁褂,比如初始化等等仆潮。在SpringBoot中故黑,ApplicationRunner
接口可以達(dá)到這樣的效果儿咱。其用法如下:
@Component
@Order(1)
public class TestRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("=======================================");
System.out.println("This is run method in ApplicationRunner");
System.out.println("=======================================");
}
}
只需要實(shí)現(xiàn)ApplicationRunner
接口的run方法即可。
如果有多個(gè)runner场晶,可以通過@Order
注解來確定執(zhí)行順序概疆。