首页 > 解决方案 > 配置错误时如何立即关闭springboot?

问题描述

在根据自动配置开发自己的组件时,我有一个问题,并且需要一些关键参数。

所以我在我的组件中有一个检查方法,@PostConstruct

如果配置错误,我希望这个方法立即打印错误日志并关闭应用程序,就像我在 springboot 的基本配置中设置错误一样。

但事实是,在使用SpringApplication.exit(applicationContext)关闭应用程序时,应用程序将保持运行并卡在某个地方而不会关闭。

异常轨迹如下所示:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-07-15 17:37:28.623 [app:application-center,traceId:,spanId:,parentId:] [main] ERROR | SpringApplication.java:837 | o.s.boot.SpringApplication | Application run failed
java.lang.IllegalStateException: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@320494b6 has been closed already
    at org.springframework.context.support.AbstractApplicationContext.assertBeanFactoryActive(AbstractApplicationContext.java:1093)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108)
    at org.springframework.cloud.context.scope.refresh.RefreshScope.eagerlyInitialize(RefreshScope.java:127)
    at org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:118)
    at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:112)
    at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:66)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:898)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at com.myapp.applicationcenter.Application.main(Application.java:24)

标签: javaspring-bootspring-boot-actuator

解决方案


嗯,注射后

    @Resource
    private ApplicationContext applicationContext

在 try catch 块中使用((ConfigurableApplicationContext)applicationContext).close(),在捕获异常后关闭应用程序将解决问题。

有没有更优雅的方式来做到这一点?


推荐阅读