是否可以从ApplicationListener关闭上下文?

   public class MyListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            // on some certain condition we want to shutdown spring ie close the context
        ((ConfigurableApplicationContext)event.getApplicationContext()).close();
        }
    }


问题是Spring仍然想在这里完成启动过程:

public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
}


因此抛出IllegalStateException:


  java.lang.IllegalStateException:
  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@dcb84c98
  已经关闭了

最佳答案

您实际上想问的问题似乎是“如何中断Spring-Boot启动”。

从您的onApplicationEvent方法引发异常。

关于java - 从ApplicationListener关闭spring应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56837551/

10-09 03:31