Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I terminate a Spring application during startup?

I have a Spring Boot application which has some external dependencies (eg. files outside the project that need to exists in order for the application to start up properly).

One of my beans has a @PostConstruct method that does the initialization. I would like to exit cleanly and gracefully if the initialization is not successful - for example, the files are not found.

Calling ((ConfigurableApplicationContext)applicationContext).close(); in the @PostConstruct method results in

java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context

and a chain of other Exceptions. Is there a way to do this properly?

like image 229
egbokul Avatar asked Jul 24 '15 09:07

egbokul


1 Answers

you can't use System.exit(0) in @PostConstruct, shutdown will waiting lock of startupShutdownMonitor, can't exit.

like image 96
Clover Avatar answered Sep 19 '22 11:09

Clover