How to properly close ApplicationContext in Spring?

I am studying Core Spring certification, and I have some information about this issue found in the provided tutorial material:

What is the preferred way to close the application context?

I know that if I have something like this:

ConfigurableApplicationContext context = … // Destroy the application context.close(); 

using the close () method in the context of objet ApplicationContext is closed and the application is destroyed.

But I think this is not the best way that I should do this.

Reading the official documentation, I found that I could also do something like this:

 context.registerShutdownHook(); 

to register a Hook shutdown with the JVM , so that the JVM starts closing the Spring phase before the JVM exits. Thus, the JVM will complete the Spring close phase.

I can read this in the documentation: it is usually not possible to call context.close() because many applications (web applications) work indefinitely. But what exactly does this last statement mean? Why does the web application work indefinitely?

So my questions are:

  • Is it possible to use the second way to close the application context also not from the web application?
  • Is context.close() preferable?

Tpx

+5
source share
2 answers

As you know, ContextLoaderListener is the one that initializes and destroys your ApplicationContext, when you shut down your server, this ContextLoaderListener contextDestroyed method is contextDestroyed .

  public void contextDestroyed(ServletContextEvent event){ closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); } 

In this closeWebApplicationContext they actually call the close method in ApplicationContext, like this

  if ((this.context instanceof ConfigurableWebApplicationContext)) { ((ConfigurableWebApplicationContext)this.context).close(); } 

This is straight from spring-web-4.1.5.jar . As can be seen from this, they use close to destroy ApplicationContext in web applications.

But registerShutdownHook used to explicitly close the IoC container in applications other than web applications, as a separate desktop application, especially when you create the ApplicationContext manually from ClassPa, thanksmlApplicationContext (or) FileSystemXmlApplicationContext (or) some other types.

This is done to free all resources used by your spring application, and to call the destroy method on your spring beans, if any.

+10
source

I can read in the documentation: it is usually not possible to call context.close () because many applications (web applications) work indefinitely. But what exactly does this last statement mean? Why does the web application work indefinitely?

The web application will run as long as it launches its application server. The application server (and not you) is suitable for starting and stopping your application correctly. This means that when the application server is stopped, the servlet context is destroyed. In a Spring application, the ContextLoaderListener class registered in web.xml listens for this event (the context is destroyed) to properly close Spring.

When using Spring outside the application server (for example, for a stand-alone application), you need to properly stop the Spring context. As you said, this can be done by explicitly calling context.close() or by registering a shutdown hook ( context.registerShutdownHook() ) that makes this call for you.

+7
source

Source: https://habr.com/ru/post/1215766/


All Articles