是否可以从 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 has been closed already
最佳答案
您真正想问的问题可能是“如何中断 Spring-Boot 启动”。
从 onApplicationEvent
方法引发异常。
关于java - 从 ApplicationListener 关闭 spring 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837551/