7

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?

egbokul
  • 3,944
  • 7
  • 36
  • 54

2 Answers2

3

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

myeongkil kim
  • 2,465
  • 4
  • 16
  • 22
Clover
  • 41
  • 4
  • uh, sht... I liked this comment half a year ago but I forgot this important thing. Now I'm opening this page and see my own like, it's fine. Thanks again. – Oleksandr Nov 15 '21 at 08:28
-1

You can use Sytem exit static method, it terminates the currently running Java Virtual Machine. the code passed indicate the termination status. by convention, a nonzero status code indicates abnormal termination.

exp:

System.exit(0);
Lho Ben
  • 2,053
  • 18
  • 35