我有一个 Spring Boot 应用程序,它具有一些外部依赖项(例如,项目外部的文件需要存在才能使应用程序正常启动)。
我的一个 bean 有 @PostConstruct
进行初始化的方法。如果初始化不成功,我想干净优雅地退出 - 例如,找不到文件。
调用 ((ConfigurableApplicationContext)applicationContext).close();
在 @PostConstruct
方法导致
java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context
以及一系列其他异常。有没有办法正确地做到这一点?
请您参考如下方法:
您不能使用 System.exit(0)
在 @PostConstruct
, 关机将等待 startupShutdownMonitor
的锁,不能退出。