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.
source share