有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在springboot应用程序启动之前实现ApplicationRunner?

通过阅读Spring-boot docs,我了解到我可以创建一个实现ApplicationRunnerCommandLineRunner的类,以便在应用程序启动之前执行代码

来自文档:

An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.

然而,我有以下课程:

@Component
public class MyClass implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    Thread.sleep(10000);
  }
}

它说应用程序在3秒内启动,而不是等待10秒启动:

2018-11-19 08:51:21.906 INFO 24872 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''

2018-11-19 08:51:21.909 INFO 24872 --- [ main] com.mycompany.Application : Started Application in 3.565 seconds (JVM running for 4.016)

我想我的课程是在另一个线程中运行的。但我觉得这并不能保证我的代码在任何可能的传入请求之前就已经执行完毕

这是正确的方法吗?我错过什么了吗


共 (0) 个答案