Spring Boot同步启动器

Tod*_*ner 5 java spring spring-boot

默认情况下,org.springframework.boot.loader.Launcher将始终在launch()方法中生成后台线程(https://github.com/spring-projects/spring-boot/blob/master/spring-boot-tools/ spring-boot-loader/src/main/java/org/springframework/boot/loader/Launcher.java#L105).有没有简单的方法来改变它在同一个线程中执行,所以我的服务启动器可以知道应用程序何时完全加载,而不是每次都立即退出成功.

lub*_*nac 0

我相信你可以像这样注册监听器:

public static void main(String[] args) {
   SpringApplication app = new SpringApplication(WsBootClientApplication.class);

   app.addListeners(new ApplicationListener<ContextStartedEvent>() {
      @Override
      public void onApplicationEvent(ContextStartedEvent event) {
         //do your stuff
      }
   });
   app.run(args);
}
Run Code Online (Sandbox Code Playgroud)

如果 ContextStartedEvent 不是适合您的事件,您可以将 ContextStartedEvent 替换为 ApplicationEvent。这是更通用的,将处理您的应用程序可以侦听的所有上下文生命周期事件。