【发布时间】:2015-10-14 21:38:30
【问题描述】:
我有一个 Spring Boot 应用程序,它有一些外部依赖项(例如,项目外部的文件需要存在才能使应用程序正常启动)。
我的一个 bean 有一个 @PostConstruct 方法来进行初始化。如果初始化不成功-例如找不到文件,我想干净而优雅地退出。
在@PostConstruct方法中调用((ConfigurableApplicationContext)applicationContext).close();会导致
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context
以及一系列其他异常。有没有办法正确地做到这一点?
【问题讨论】:
-
只要抛出一个异常...
-
我只是打电话给
System.exit(0)。但我想要一个更好的解决方案。 -
System.exit(0) 是终止的“唯一”解决方案......
标签: spring spring-boot