java - 从 ApplicationListener 关闭 spring 应用程序

标签 java spring spring-boot

是否可以从 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/

相关文章:

java - 如何在 spring 中从组件扫描注释中排除测试文件夹

java.lang.NoSuchMethodError : org. springframework.util.ReflectionUtils.accessibleConstructor

java - 错误 org.hibernate.util.JDBCExceptionReporter - 通信链路故障

java - Apache Camel + RabbitMq - Camel 定义了自己的队列,并且不会从已定义的队列中读取

c# - JNBridge可以在linux下使用吗?

Java:找到大写字母时拆分字符串

java - 使用apache poi下载Excel

java - Spring 启动 : Exclude auto configuration based on environment variable

java - 将 drools guvnor 5.5 升级到 drools 6.0

security - 如何为 spring RestTemplate 设置可信证书