I am studying for the Spring Core certification and I have some dount about on this question finded into the provided study material:
What is the preferred way to close an application context?
I know that if I have something like this:
ConfigurableApplicationContext context = …
// Destroy the application
context.close();
by the use of the close() method on the context objet the ApplicationContext is closed and the application is destroyed.
But I think that this is not the best way that I have to do it.
Reading the official documentation I find that I can also do something like this:
context.registerShutdownHook();
that register a Shutdown Hook with the JVM so it is the JVM that will trigger Spring's close phase before JVM exits. So on JVM exit, Spring's close phase will execute.
On the documentation I can read that: usually not possible to call context.close()
because many applications (web applications) run indefinitely But what exactly means this last assertion? why web application run indefinitely?
So my questions are:
- Can I use this second way to close an application context also with not web application?
- Is it prefered respect the
context.close()
?
Tnx