java - 配置错误时如何立即关闭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)
解决方案
嗯,注射后
@Resource
private ApplicationContext applicationContext
在 try catch 块中使用((ConfigurableApplicationContext)applicationContext).close()
,在捕获异常后关闭应用程序将解决问题。
有没有更优雅的方式来做到这一点?
推荐阅读
- jakarta-ee - 如何避免wildfly为JNDI创建依赖项
- javascript - 在堆栈导航器中保存 Switch 组件的状态
- python - 搜索 JSON 文件以获取记录并附加到数据框
- angular - Angular 7 中的单例服务
- regex - 以空格分隔的多个匹配项
- javascript - 如何调用和打印 nodeType 名称而不仅仅是数字(例如:“ELEMENT_NODE”)?
- java - 将 N 表示为 1、3 之和的不同方式的计数
- coffeescript - 如何清理文件名中的 HTML - Javascript Coffeescript
- java - Java gRPC 客户端预测对 half_plus_two 示例模型的调用
- python - 无法使用 Pafy/youtube_dl 初始化新 URL,我收到“错误:此视频不可用”