美文网首页程序员
Spring-boot: 容器启动后执行指定方法

Spring-boot: 容器启动后执行指定方法

作者: KAMIWei | 来源:发表于2018-02-05 10:15 被阅读346次

Spring-boot给我们提供了两种在容器启动后执行指定方法的接口: ApplicationRunnerCommandLineRunner.

  • 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
  }
}

相关文章

网友评论

    本文标题:Spring-boot: 容器启动后执行指定方法

    本文链接:https://www.haomeiwen.com/subject/badbzxtx.html