When some Web Application Shut downs Spring's web-based ApplicationContext handles shutting down gracefully but for a non web application if you want the container to shutdown gracefully and call the relevant destroy callbacks on your singleton beans, you will need to register a shutdown hook with the JVM.

So for registering a shutdown hook that enables the graceful shutdown for the relevant Spring IoC container, we simply need to call the registerShutdownHook () method that is declared on the AbstractApplicationContext class.

Java Code:
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public final class Test {

     public static void main(final String[] args) throws Exception {

           AbstractApplicationContext ctx= new ClassPathXmlApplicationContext(
                      new String []{"test.xml"});

           ctx.registerShutdownHook();
        
           // app runs here...
           // main method exits, hook is called prior to the app shutting down...
     }
}