Spring-boot给我们提供了两种在容器启动后执行指定方法的接口: ApplicationRunner
和CommandLineRunner
.
-
CommandLineRunner
: 通过字符串数组接收参数 -
ApplicationRunner
: 通过ApplicationArguments 接收参数
示例:
@Component
@Order(value = 1) // 决定各个Runner的执行次序
public class MyCommandLineRunner implements CommandLineRunner{
@Override
public void run(String... var1) throws Exception{
// do something
}
}
@Component
@Order(value = 2) // 决定各个Runner的执行次序
public class MyApplicationRunner implements ApplicationRunner{
@Override
public void run(ApplicationArguments var1) throws Exception{
// do something
}
}
网友评论