SpringBoot的ApplicationRunner

2020-06-28  本文已影响0人  JacobY

在开发的过程中,有时候需要在应用启动后去自动进行一些操作,比如初始化等等。在SpringBoot中,ApplicationRunner接口可以达到这样的效果。其用法如下:

@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("=======================================");
    }
}

只需要实现ApplicationRunner接口的run方法即可。
如果有多个runner,可以通过@Order注解来确定执行顺序。

上一篇下一篇

猜你喜欢

热点阅读